Scala def in an object - naming convention upper case camel case? -
given conventions here: http://docs.scala-lang.org/style/naming-conventions.html
blockquote constant names should in upper camel case. is, if member final, immutable , belongs package object or object, may considered constant (similar java’s static final members)
does mean def should fall in category too? if functionally pure. example parse method:
object parser{def parse(string: string): anyref = ??? }
no, shouldn't.
that explanation simplification of concept of stable value. stable value can trust have same value, , how scala introduces dependent types (aka, in scala, path-dependent types).
there number of rules stable value, why simpler explanation resorted in style guide. 1 specific rule has be val
-- var
, def
not acceptable. var
not acceptable because value can change @ time, , def
not acceptable because may return different values each time called (even if receives no parameters).
also related fact can override def
val
, not vice versa.
so val
qualifies constants.
also of interest, scala optimizes final val declarations when not declare type, this:
object constants { final val 0 = 0 }
that cause scala replace instances of zero
0
. in fact, if recompile constant
changing value of zero
0
else, code making reference constants.zero
has been compiled before still use 0
.
if, on other hand, had declared final val zero: int = 0
, not happen.
Comments
Post a Comment