Minor improvement of the factory method

2 views
Skip to first unread message

Dmitry Dobrynin

unread,
Mar 10, 2011, 5:33:08 PM3/10/11
to mongo-scala-driver
Hi Alexander

It would be more convenient If the signature of ObjectIn.factory(dbo:
DBObject): Option[T] is to be changed as follows:
def factory(dbo: Option[DBObject]): Option[T]

Firstly the argument holder comforms the output holder, secondly users
will not instantiate several Some(dbo)s (of cause Some(dbo) can be
instantiated before the for). Furthermore code is clearer.

F.i.
def factory(dbo: DBObject): Option[User] = for {
name(n) <- Some(dbo)
lastName(l) <- Some(dbo)
email(e) <- Some(dbo)
} yield new User(n, l, e)

become

def factory(dbo: Option[DBObject]): Option[User] = for {name(n) <-
dbo; lastName(l) <- dbo; email(e) <- dbo} yield new User(n, l, e)

I've changed the method and can send you the patch if it's worth. All
tests passed.

Regards,
Dmitry

Alexander Azarov

unread,
Mar 11, 2011, 1:39:05 AM3/11/11
to mongod...@googlegroups.com
Dmitry,

But the truth is, that "factory" must accept any DBObject, but in no event it gets "none", so this fact is translated to the method signature.

And by the way, if you have "Option" fields, you don't wrap "dbo" into "Some()", e.g.

yield new Obj(...., field from dbo, ....)

I appreciate your readiness to send patches! -- register at GitHub and fork the project, this way you'll be able to send "pull requests" very easily

Best,
Alexander

Dmitry Dobrynin

unread,
Mar 11, 2011, 3:38:10 AM3/11/11
to mongo-scala-driver
I don't see any issue to change from(dbo: DBObject) to use the
Option:
def from(dbo: Option[DBObject]) = unapply(dbo.get). As you are
developing a library you can do such an excess to provide syntactical
convenience.
By contract the factory method and the from method never get None as
the argument.

Regards,
Dmitry
Reply all
Reply to author
Forward
0 new messages