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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -