Skip to content

Commit 3e9c566

Browse files
committed
enhanced documentation about super
1 parent 0d232f9 commit 3e9c566

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

docs/reference.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,38 @@ properties and methods from another class.
606606
super name
607607

608608
Here we extend our Inventory class, and limit the amount of items it can carry.
609-
The `super` keyword can be called as a function to call the function of the
610-
same name in the super class. It can also be accessed like an object in order
611-
to retrieve values in the parent class that might have been shadowed by the
612-
child class.
609+
610+
### Super
611+
612+
`super` is a special keyword that can be used in two different ways: It can be
613+
treated as an object, or it can be called like a function. It only has special
614+
functionality when inside a class.
615+
616+
When called as a function, it will call the function of the same name in the
617+
parent class. The current `self` will automatically be passed as the first
618+
argument. (As seen in the [inheritance](#inheritance) example above)
619+
620+
When `super` is used as a normal value, it is a reference to the parent class
621+
object.
622+
623+
It can be accessed like any of object in order to retrieve values in the
624+
parent class that might have been shadowed by the child class.
625+
626+
When the `\` calling operator is used with `super`, `self` is inserted as the
627+
first argument instead of the value of `super` itself. When using `.` to
628+
retrieve a function, the raw function is returned.
629+
630+
A few examples of using `super` in different ways:
631+
632+
class MyClass extends ParentClass
633+
a_method: =>
634+
-- the following have the same effect:
635+
super "hello", "world"
636+
super\a_method "hello", "world"
637+
super.a_method self, "hello", "world"
638+
639+
-- super as a value is equal to the parent class:
640+
assert super == ParentClass
613641

614642
### Types
615643

0 commit comments

Comments
 (0)