guild icon
Toit
#How to call a function of the base class?
Thread channel in help
kaxori
kaxori 06/13/2025 04:46 PM
If a derived class and its base class have both the same function implemented. But if the base calls its function the function of the derived class is executed. How can I force to call the base class function ?
floitsch
floitsch 06/13/2025 04:47 PM
You can call super.
example:
class A: foo x: print "from A: $x" class B extends A: foo x: super (x + 1)
kaxori
kaxori 06/13/2025 05:05 PM
class Base: f m/string: print "Base: $m" b: f "b" class Derived extends Base: f m/string: print "Derived: $m" d: b main: c := Derived c.d

What I expected is that b calls Base, but what is get is "Derived"
kaxori
kaxori 06/13/2025 05:05 PM
class A can't know that it is derived.
floitsch
floitsch 06/13/2025 05:05 PM
every call in Toit is virtual, except for direct super calls.
floitsch
floitsch 06/13/2025 05:06 PM
Extending classes is one of the most tight ways of binding two different classes. It should be done with care.
floitsch
floitsch 06/13/2025 05:07 PM
this is just one example, but there are many others, where changing the base or derived class can lead to unexpected behavior.
kaxori
kaxori 06/13/2025 05:07 PM
Aha: i use the function to log things. And now wondered that always the derived log-ID appears.
floitsch
floitsch 06/13/2025 05:07 PM
:๐Ÿ™‚:
kaxori
kaxori 06/13/2025 05:09 PM
every call in Toit is virtual that is new for me and now the behaviour makes sense.
Thank you
kaxori
kaxori 06/13/2025 05:11 PM
I need to read a language concept doc ...
bitphlipphar
bitphlipphar 06/14/2025 06:21 AM
You could make the f functions static?(edited)
bitphlipphar
bitphlipphar 06/14/2025 06:24 AM
class Base: static f m/string: print "Base: $m" b: f "b" class Derived extends Base: static f m/string: print "Derived: $m" d: b main: c := Derived c.d
floitschfloitsch
every call in Toit is virtual, except for direct super calls.
bitphlipphar
bitphlipphar 06/14/2025 06:25 AM
... and except for calls to top-level or static functions.
๐Ÿ‘๐Ÿป1
bitphlipphar
bitphlipphar 06/14/2025 06:26 AM
The downside to making these static is that you have to pass the instance (this) to f if you want to access the instance from within f.
kaxori
kaxori 06/14/2025 09:51 AM
Thank you.
Where can I find the holy toit bible :๐Ÿ˜‡: ?
floitsch
floitsch 06/14/2025 09:52 AM
Some of it is here: https://docs.toit.io/language
Probably still some things missing.
The object model is also relatively similar to Java's and/or Dart's.
(edited)
kaxori
kaxori 06/14/2025 09:57 AM
I drink coffee and I play darts :๐Ÿคช:
grew up reading books (Kernighan, Ritchy, Stroustrup) :๐Ÿค“:
floitsch
floitsch 06/14/2025 09:57 AM
Java, C#, Dart, and Toit all have pretty similar (core) object models.
floitsch
floitsch 06/14/2025 09:59 AM
With time, they add more features (extension methods, ...), but at their core they still are the same: classes with interfaces, and all method calls are virtual.
kaxori
kaxori 06/14/2025 10:01 AM
...and the way (making you happy) to program ยตC's is Toit!
21 messages in total