Scala type-safety vs overhead (type vs class) -
i have graph, each vertex has both id (which never changes) , label (which changes frequently). both represented longs.
currently, define following types:
type vertexid = long type vertexlabel = long
however, discovered bug today passing in vertexlabel function expected vertexid. seems type of thing scala compiler should able prevent.
i considered doing instead:
case class vertexid(id: long) case class vertexlabel(label: long)
but there's overhead of "boxing" , "unboxing" , memory usage.
is there anyway best of both worlds? define both types longs in such way compiler prevent using 1 other?
yes, can use value classes:
case class vertexid(val id: long) extends anyval case class vertexlabel(val label: long) extends anyval
see http://docs.scala-lang.org/overviews/core/value-classes.html
Comments
Post a Comment