Reputation: 1254
A powerful feature of Fantom programming language is the it-block constructor, often used in serializable classes. Unfortunately I cannot find documentation detailed enough to do what I though should be pretty simple: Extending a class that declares this type of constructor. The best documentation I've found so far is in this post, but doesn't show an example of what I'm trying to do.
Let's say I have these classes:
class Animal {
protected const Str name
new make( |This| f ) { f(this) }
override Str toStr() { "I'm an Animal and my name is $name" }
}
class Main {
Void main() {
a := Animal {
name = "Flipper"
}
echo( a )
}
}
So far so good, it prints: "I'm an Animal and my name is Flipper". Now I want to extend Animal with this class:
class Dog : Animal {
override Str toStr() { "I'm an Dog and my name is $name" }
}
But Fantom compiler says:
Must call super class constructor in 'make'
So I changed the class to be:
class Dog : Animal {
new make( |This| f ) : super( this ) { f(this) }
override Str toStr() { "I'm an Dog and my name is $name" }
}
But now the compiler complains:
invalid args make(|Playground::Animal->sys::Void|), not (Playground::Dog)
which makes sense since I'm passing a Dog instance, instead of an Animal, so what should I be passing to the super constructor then?
Upvotes: 1
Views: 83
Reputation: 1254
After giving a bit more thought to the compiler error I realised all what I had to pass was f:
class Dog : Animal {
new make( |This| f ) : super( f ) { }
override Str toStr() { "I'm an Dog and my name is $name" }
}
I hope this helps others.
Upvotes: 1