I have found for self is very interesting a fact. For example i’ve wrote:
type A = { val st: Set[Any]
val start: Set[Any]
val Sigma : Set[Char]
def tr(f: (Tuple2[Any, Any])=>Boolean): Set[Any]
}
class Fon {
val st: Set[Any]
val start: Set[Any]
val Sigma : Set[Char]
def tr(f: (Tuple2[Any, Any])=>Boolean): Set[Any] = Set(out)
def out: String = "is just example"
}
val a: A = new Fon
a.tr(f(Tuple2('a',0)))
But if i will try do call a.out – i get an error, that the type A have not exist ‘out’
What happening to this, how this to work?
Thanks.
There is no such method as
A.out, because of how you’ve defined theAtype. Thus, when you try to call a method calledouton an object of typeA, the compiler correctly tells you that no such method exists.This is not related to structural typing, by the way – if you had made
Aa trait and hadFonextend it, you’d run into exactly the same problems. Moreover, this is just how static typing systems work – the compiler can’t guarantee that your code is typesafe so it won’t compile it.If you want to call the
outmethod, then you’ll need to refer to that object via aFonvariable: