scala - accessing constructor variable in companion object -
getting compile time error following classes in same file
class fancygreeting (greeting: string) { //private var greeting: string=_; def greet() = { println( "greeting in class" + greeting) } } object fancygreeting { def privategreeting(f:fancygreeting) : string = { f.greeting; } }
error: value greeting not member of this.fancygreeting f.greeting;
the same works if use private variable greeting instead of constructor
you should write class fancygreeting(private var greeting: string) {
if want have same behavior when use line commented out. way write (i.e. class fancygreeting(greeting: string) {
) giving greeting
parameter constructor, without making property.
this said, should not use ";" end lines in scala. moreover, better use val
var
, if can.
note: this answer might interesting you.
Comments
Post a Comment