A trait is a mechanism for the re-use of code in a language with single inheritance, such as ViviFire. A trait can decrease some of the limits of single inheritance. Traits let you re-use sets of methods freely in different classes, related or not. The effects of the combination of traits and classes are specified in a manner which is easy to understand. These effects can prevent the problems with multiple inheritence and mixins.
A trait looks almost the same as a class, but specifies only behavior in a regular manner. You can not make an instance of a trait. Along side the usual inheritance mechanism, traits permit what is referred to as "horizontal composition" of behavior. That is, the trait applies its methods to a class without unnecessary inheritance.
Declaration of traits
A trait usually starts with Trait and ends with End Trait. Between these two lines you can put one or more allowed 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 has no direct access to any of the other members of the trait.
A method or property is usually defined with a body of statements. But, if you use the Abstract modifier, the method or property can not have a body. A class that uses the trait must redeclare the method or property and supply its own body of statements.
If a class and a trait expose different methods with the same signature, the class comes before the trait. And if a trait and a superclass cause the same problem, the trait comes before the superclass.
That is, class > trait > superclass.
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
Changing 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
If all of the methods and properties of a trait must be abstract, you can put the Abstract modifier before the trait. Then it is not necessary to put the modifier before each method and property.
An abstract trait has more limitations than a normal trait. It must have Method or Property signatures. It must not have Function and Sub procedures.
Shared trait members
Uses for traits
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