MongoListField saves list of case class instances as strings

36 views
Skip to first unread message

Erik Allik

unread,
Apr 10, 2014, 6:04:21 PM4/10/14
to lif...@googlegroups.com
When saving a list of case class instances into a MongoListField, they're saved to the DB as strings, and also loaded back as such.

This seems to be a bug. Furthermore, I'm also of the opinion that a framework written in a statically typed language should be able to produce a compilation error in cases like these, unless there's some extraneous dynamism going on in its core, but I also acknowledge that I'm in no position to criticize how the Record framework's been designed.

The code to reproduce the issue is attached, or can be viewed at http://pastebin.com/haahzKYf

Regards,
Erik Allik

foodb.scala

Antonio Salazar Cardozo

unread,
Apr 10, 2014, 7:32:05 PM4/10/14
to lif...@googlegroups.com
Wat. So what we're saying here is when you load it back you actually get a list of String instead
of a list of Bar? But at compile time it essentially appears to be a List of Bar?

For what it's worth, if that's what we're talking about, (a) there is extraneous dynamism (populating
a list from an external entity where the objects have been serialized at runtime is not type-checked
at compile time, because it pretty much has to use reflection unless it uses Java serialization, which
is super-brittle and nasty) and (b) we don't get a runtime error at deserialization time because the
JVM erases types at runtime, so there's no check on the types of objects that go into a collection,
even if their types were specified at compile-time.
Thanks,
Antonio

Tim Nelson

unread,
Apr 11, 2014, 6:21:42 AM4/11/14
to lif...@googlegroups.com
MongoListField only supports basic types. If you want to use case classes you need to use the MongoCaseClassListField, or as I prefer, BosnRecords with BsonRecordListField.

Tim

Antonio Salazar Cardozo

unread,
Apr 11, 2014, 10:44:04 AM4/11/14
to lif...@googlegroups.com
So the followup question is, is there a way that we can encode that in the type system so you don't find
yourself accidentally using MogoListField when you want MongoCaseClassListField? What does “basic
types” mean? Can we require a T <: AnyVal or something like that?
Thanks,
Antonio

Tim Nelson

unread,
Apr 11, 2014, 11:22:46 AM4/11/14
to lif...@googlegroups.com
I've been wondering if there was a way to basically require a type to be one from a list of types.

The types that are supported are those checked in the asDBObject function [1]. The functions are defined in Meta.scala [2].

There are a bunch of Java types that are supported, so I don't think AnyVal would work. 

Tim

Antonio Salazar Cardozo

unread,
Apr 11, 2014, 11:36:24 AM4/11/14
to lif...@googlegroups.com
There are some small tricks to create type unions, as per http://www.chuusai.com/2011/06/09/scala-union-types-curry-howard/:

  type ¬[T] = T => Nothing
  type ¬¬[T] = ¬[¬[T]]
  type [T, U] = T with U
  type [T, U] = ¬[¬[T] ¬[U]]

Towards the end of the post, you see how we would implement a signature with implicit parameter that
would require the type to be one of a list of types. Not 100% sure this is a good idea (it certainly makes
the code a bit harder to follow), and I'd be curious to hear others' thoughts on this.

In this case it definitely looks like it could be handy though.
Thanks,
Antonio
Reply all
Reply to author
Forward
0 new messages