scala - How does the object in class pattern work, as used in the Lift Framework? -


i'm new scala , can't head around how lift guys implemented record api. however, question less api more scala in general. i'm interested in how object in class pattern works, used in lift.

class maindoc private() extends mongorecord[maindoc] objectidpk[maindoc] {   def meta = maindoc    object name extends stringfield(this, 12)   object cnt extends intfield(this) }  object maindoc extends maindoc mongometarecord[maindoc] 

in upper snippet can see how record defined in lift. interesting part fields defined objects. api allows create instances this:

val md1 = maindoc.createrecord   .name("md1")   .cnt(5)   .save 

this done using apply method? @ same time able values doing this:

val name = md1.name 

how work? objects not static when in scope of class. or constructor classes internal representation? how possible iterate on fields, use reflection?

thanks, otto

you're right being apply method. record's field base class defines few apply methods.

def apply(in: box[mytype]): ownertype def apply(in: mytype): ownertype 

by returning ownertype, can chain invocations together.

regarding use of object define fields, confused me @ first, too. object identifier defines object within particular scope. though it's convenient think of object shortcut singleton pattern, it's more flexible that. according scala language spec (section 5.4):

it equivalent following definition of lazy value:
lazy val m = new sc mt1 ... mtn { this: m.type => stats }

<snip/>

the expansion given above not accurate top-level objects. cannot because variable , method definition cannot appear on top-level outside of package object (§9.3). instead, top-level objects translated static fields.

regarding iterating on fields, record objects define allfields method returns list[net.liftweb.record.field[_, mytype]].


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -