I have created a simple application with singleton object which contains local traits:
object Singleton {
trait FirstTrait {
val func1 = (i: Int) => i * 2
}
trait SecondTrait {
val func2 = (s: String) => s
}
trait ThirdTrait {
val func3 = () => println("Func 3")
}
}
And now, in Main object, I would like to do something like this:
object Main extends App {
val singleton = Singleton.FirstTrait//cannot do this
}
But I cannot do this (compile error). Why? Why I have not an access into this local trait? If I change Singleton object into:
object Singleton {
trait FirstTrait {
val func1 = (i: Int) => i * 2
}
trait SecondTrait {
val func2 = (s: String) => s
}
trait ThirdTrait {
val func3 = () => println("Func 3")
}
object FirstObject extends FirstTrait {
println(func1)
}
}
Main works well and program compiles. But I call another singleton object from Singleton, not a trait. I understand that trait cannot be instanced, but I think it is not a solution for it, because I have also a simple ScalaTest test name, which looks like
"Singleton" should "test it" in Singleton.FirstTrait{...}
and here I have an access into FirstTrait. So why I cannot use it in normal code?
I cannot understand it well. Maybe I am an idiot, but if someone could explain it to me well, I would be greatful.