Skip to content

Commit 3249a7c

Browse files
committed
docs: self type
1 parent 87cc01f commit 3249a7c

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

docs/tutorial.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ local interface MyAbstractInterface
578578
x: integer
579579
y: integer
580580
my_func: function(self, integer)
581+
another_func: function(self, integer, self)
581582
end
582583

583584
MyAbstractInterface.a = "this doesn't work" -- error!
@@ -606,14 +607,26 @@ r.b = "this works"
606607
r.a = "this works too because 'a' comes from MyAbstractInterface"
607608
```
608609

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.
615615

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:
616622

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+
```
617630

618631
## Generics
619632

0 commit comments

Comments
 (0)