I am writing a small record processing application.
To do this, I am defining a Cursor (InputCursor, OutputCursor) class. An
Accessor class will access the data. The Accessor is initialized with the
Cursor so as the cursor advances, the accessor will be able to access that
record's data. The data will be manipulated by the accessor. So think of
something like the following:
val inputCusor = ...
val outputCursor = ...
// Assume that through the cursors I can access the current data record.
val ageAccessor: new FieldAccessor[Int](inputCursor)
val outputAgeAccessor: new FieldAccessor[Int](outputCursor)
while(!inputCursor.get) {
outputAgeAccessor() = ageAccessor().getOrElse(0).map { _ + 10 }// age
everyone 10 years or set them to 10 years if its null
outputCursor.putRecord()
So I need to ensure that the ageAccessor only accesses an Int field in the
input data. If I assume that the data is stored in a value array (the
record) and that I know that position 1 is the age or null where is my type
safety since almost no matter what, the value in the record is only known
to be a Any value, I have to do a cast.
Let's say that:
/**
* Access a specific field in the cursor.
*/
trait Accessor[T] {
def apply(): Option[T]
def update(value: T): Unit
/**
* A type class of accessors.
*/
class FieldAccessor[T](val cursor: Cursor) extends Accessor[T] {
def apply(): Option[T] = ... // access the data held by the cursor but
have to do a cast
def update(value: T) = ... // access the data held by the cursor but I
lose type info
So a couple of questions:
a) I do no seem to be able to enhance my type safety all the way down to a
the actual data storage level. Is there a way?
b) How do I make my update method take an Option[T] and still allow me to
write outputAgeAccessor() = 10? I keep getting a compiler error if I try to
make update take a Option[T] value. Do I need to make FieldAccessor a
monoid? (I do not really know what a monoid is yet, but it seems like it is
suppose to help me solve this type of problem e.g. access to a value in a
container).
c) Do I just need to rethink the entire approach because I am thinking too
java-ish?
To do this, I am defining a Cursor (InputCursor, OutputCursor) class. An
Accessor class will access the data. The Accessor is initialized with the
Cursor so as the cursor advances, the accessor will be able to access that
record's data. The data will be manipulated by the accessor. So think of
something like the following:
val inputCusor = ...
val outputCursor = ...
// Assume that through the cursors I can access the current data record.
val ageAccessor: new FieldAccessor[Int](inputCursor)
val outputAgeAccessor: new FieldAccessor[Int](outputCursor)
while(!inputCursor.get) {
outputAgeAccessor() = ageAccessor().getOrElse(0).map { _ + 10 }// age
everyone 10 years or set them to 10 years if its null
outputCursor.putRecord()
So I need to ensure that the ageAccessor only accesses an Int field in the
input data. If I assume that the data is stored in a value array (the
record) and that I know that position 1 is the age or null where is my type
safety since almost no matter what, the value in the record is only known
to be a Any value, I have to do a cast.
Let's say that:
/**
* Access a specific field in the cursor.
*/
trait Accessor[T] {
def apply(): Option[T]
def update(value: T): Unit
/**
* A type class of accessors.
*/
class FieldAccessor[T](val cursor: Cursor) extends Accessor[T] {
def apply(): Option[T] = ... // access the data held by the cursor but
have to do a cast
def update(value: T) = ... // access the data held by the cursor but I
lose type info
So a couple of questions:
a) I do no seem to be able to enhance my type safety all the way down to a
the actual data storage level. Is there a way?
b) How do I make my update method take an Option[T] and still allow me to
write outputAgeAccessor() = 10? I keep getting a compiler error if I try to
make update take a Option[T] value. Do I need to make FieldAccessor a
monoid? (I do not really know what a monoid is yet, but it seems like it is
suppose to help me solve this type of problem e.g. access to a value in a
container).
c) Do I just need to rethink the entire approach because I am thinking too
java-ish?