-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This takes the better manual documentation from the nit website. Signed-off-by: Hugo Leblanc <[email protected]>
- Loading branch information
Showing
1 changed file
with
45 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,52 @@ | ||
# Virtual Types | ||
|
||
`type` declares a virtual types in a class. A bound type is | ||
mandatory. Virtual types can then be used as regular types in the class | ||
and its subclasses. Subclasses can also redefine it with a more specific | ||
bound type. One can see a virtual type as an internal formal generic | ||
parameter or as a redefinable *typedef*. | ||
Virtual type is a way to define in a class a property that is associated with a type. | ||
This property can be used to type parameters, return of functions, variables, etc. | ||
Virtual types are inherited by subclasses and can be redefined. | ||
|
||
Virtual types is a good solution when: | ||
|
||
* A class need to refer to the real itself | ||
* Two class hierarchy are somewhat parallel | ||
* You want covariance | ||
|
||
### Usage | ||
|
||
Example, we have employees who work in standard office and bosses, that are technically employees, but they work in boss offices (that are improved offices with a fridge). | ||
|
||
One way to model this is to use virtual types. | ||
Virtual types are defined inside class (like methods and attributes) but with the keyword `type`. | ||
Inside the class, the virtual type can be used (almost) like any other static type. | ||
|
||
~~~ | ||
class Employee | ||
type OFFICE: Office | ||
var office: OFFICE | ||
end | ||
class Office | ||
# ... | ||
end | ||
~~~ | ||
|
||
Subclasses can redefine (with `redef`) the virtual types they want to change. | ||
The only requirement is that the new bound is a sub-type of the previous bound. | ||
|
||
~~~ | ||
class Foo | ||
type E: Object | ||
var derp: E | ||
class Boss | ||
super Employee | ||
redef type OFFICE: BossOffice | ||
end | ||
class Bar | ||
super Foo | ||
redef type E: Int | ||
class BossOffice | ||
super Office | ||
end | ||
var b = new Bar(5) | ||
print b.derp + 1 # outputs 6 | ||
~~~ | ||
|
||
The redefinition of a virtual type is used by the compiler to prevent some type errors. | ||
For example, assuming that boss' offices have a fridge: | ||
|
||
~~~nitish | ||
var e: Employee = ... | ||
e.office.fridge.open # Compilation Error! Office has no method fridge | ||
var b: Boss = ... | ||
b.office.fridge.open # OK! | ||
~~~ |