Traits
A trait is a construct that lets you use code again and again. In a language that does not let classes inherit from more than one parent class, (which ViviFire is one), traits can decrease some of the limits this causes. Traits let you use sets of methods freely in different classes, related or not. The effects of the mixture of traits and classes are specified in a manner which is easy to accept.
A trait shows almost the same as a class, but specifies only behavior in a controlled manner. You cannot make a variable from a trait. Together with the usual inheritance construct, traits do what is known as horizontal composition of behavior. That is, the trait applies its methods to a class 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 Does 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 Does 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 Does Swim Does Speak
@Override Method Speak As String
Return "Arf"
End Method
End Class
Class Cat Is Pet Does Walk Does Speak
@Override Method Speak As String
Return "Meow"
End Method
End Class
Class Fish Is Pet Does Swim
End Class