I want to do some funky closure-like stuff. I want a method to return an anonymous object whose guts make reference to the parameters of the method. Here is the code that I wrote that illustrates my intent:
object SessionManagement {
implicit class SessionManagementExtensions( val c : ChainBuilder ) {
def set( dest: String ) = object {
def from( src: String ) =
c.exec( session => {
val list = session( src ).as[Vector[String]]
val i = if ( list.size == 0 ) -1 else Random.nextInt( list.size )
val value = if ( i > 0 ) list(i) else "INVALID_" + dest
session.set( dest, value )
})
def to[T]( v: Expression[T] ) =
c.exec( session => session.set( dest, v ) )
}
}
What I'm TRYING to do is have a call to "set" return an object that allows me to then chain together a call to ".to", like so:
.set( SOMETHING ).to( OTHER )
But I can't say
def foo = object { ... }
Is there a way in Scala to get what I am after? Do I have to define a class and instantiate it?