Traits
ViviFire lets you use code again and again with a construct known as a Trait. Traits are a possible alternative to multiple inheritance (a Class with many parents). But they can also decrease some limits of single inheritance.
Traits let you use sets of methods again and again without limits in many classes independently. And such classes do not have to be related through inheritance.
The semantics of traits and classes put together is made to be easy to use. It also does not have the typical problems related to multiple inheritance and Mixins.
A trait looks almost the same as a class. But its primary function is to make a group of operations that are usually small and fully related.
It is not possible to make instances of a trait directly. Different from the usual inheritance, it lets you do horizontal composition of behavior. In other words, it applies class members without inheritance.
Declaration of traits
A trait usually starts with Trait
and is completed with End Trait
.
Between these two lines you can put one or more permitted statements:
Method
, Property
, Function
, Sub
, and TBD.
A class that uses a trait has access only to the public members of the trait—methods and properties. These also become part of the interface of the class . The class cannot directly get access to the other members of the trait.
A method or property is usually made with a body of statements.
But if you use the modifier @Abstract
, the method or property cannot have a body.
A class that uses the trait must declare the method or property again, and supply a body of statements.
Multiple traits
TODO
When different methods have the same signature
You have an example class that applies one or more traits. If that class has two or more methods with the same signature, the result is a compiler error. You can change the names of the methods to correct this error.
Trait A
Method Calculate
' ...
End Method
End Trait
Trait B
Method Calculate
' ...
End Method
End Trait
Class Calculator Does A, B
Method CalculateA Does A.Calculate
Method CalculateB Does B.Calculate
End Class
To Change method visibility
Trait Foobar
Method Foo
' ...
End Method
Method Bar
' ...
End Method
End Trait
Class Barless Does Foobar
Function Bar Does Foobar.Bar
End Class
Traits composed from traits
Trait Foo
Method Foo
' ...
End Method
End Trait
Trait Bar
Method Bar
' ...
End Method
End Trait
Trait Foobar Does Foo, Bar
' ...
End Trait
Abstract traits
If all of the methods and properties of a trait must be abstract, you can put the modifier @Abstract
before the trait.
Then it is not necessary to put the modifier before each method and property.
An abstract trait has more limits than a usual trait:
- It must have one or more public procedures (
Method
orProperty
). - It must not have private procedures (
Function
andSub
).
Shared trait members
TODO
Properties
TODO
Uses for traits
TODO
Examples
@Abstract Class Pet
End Class
Trait Walk
Method StartWalking
End Method
End Trait
Trait Swim
Method StartSwimming
End Method
End Trait
@Abstract Trait Speak
Method Speak As String
End Trait
Class Dog Is Pet Does Walk, Swim, Speak
@Override Method Speak As String
Return "Arf"
End Method
End Class
Class Cat Is Pet Does Walk, Speak
@Override Method Speak As String
Return "Meow"
End Method
End Class
Class Fish Is Pet Does Swim
End Class