@@ -578,6 +578,7 @@ local interface MyAbstractInterface
578
578
x : integer
579
579
y : integer
580
580
my_func : function (self , integer )
581
+ another_func : function (self , integer , self )
581
582
end
582
583
583
584
MyAbstractInterface .a = " this doesn't work" -- error!
@@ -606,14 +607,26 @@ r.b = "this works"
606
607
r .a = " this works too because 'a' comes from MyAbstractInterface"
607
608
```
608
609
609
- Note that this refers strictly to subtyping of interfaces, not
610
- inheritance of implementations. You cannot use ` is ` to do
611
- ` local MyRecord is AnotherRecord ` , as Teal does not implement
612
- a class/object model of its own, as it aims to be compatible
613
- with the multiple class/object models that exist in the Lua
614
- ecosystem.
610
+ Keep in mind that this refers strictly to subtyping of interfaces, not
611
+ inheritance of implementations. You cannot use ` is ` to do `local MyRecord is
612
+ AnotherRecord`, as Teal does not implement a class/object model of its own, as
613
+ it aims to be compatible with the multiple class/object models that exist in
614
+ the Lua ecosystem.
615
615
616
+ Note also that the definition of ` my_func ` used ` self ` as a type name. ` self `
617
+ is a valid type that can be used when declaring arguments in functions
618
+ declared in interfaces and records. When a record is declared to be a subtype
619
+ of an interface using ` is ` , any function arguments using ` self ` in the parent
620
+ interface type will then resolve to the child record's type. The type signature
621
+ of ` another_func ` makes it even more evident:
616
622
623
+ ``` lua
624
+ -- the following function complies to the type declared for `another_func`
625
+ -- in MyAbstractInterface, because MyRecord is the `self` type in this context
626
+ function MyRecord :another_func (n : integer , another : MyRecord )
627
+ print (n + self .x , another .b )
628
+ end
629
+ ```
617
630
618
631
## Generics
619
632
0 commit comments