Edit Rename Changes History Upload Download Back to Top

VWTraits - A Traits implementation for VW

VW Traits is an implementation of Traits for VisualWorks. However, it is not a strict Traits implementation. Traits programming is a techinque of code composition and code reuse. A Trait is a partial implementation of a class, multiple traits are combined together, along with glue code, to comprise a class (client). You can view the original Trait papers here.

For various reasons I implemented VW Traits so that Traits are direct subclasses of the VWTrait class, the effects of deeper subclassing are not defined. Furthermore, a trait class is not intended to produce viable instances. However, one may find it useful to do unit testing on a trait instance.

Important points about VWTraits

  1. VWTraits can include state, instance variables, class instance variables, and shared variables.
  2. Methods, i.e. CompiledMethods, are not reused. Instead the source is copied from the trait class and compiled into the client class.
  3. Changing a trait method in the client class will update the trait method and all clients of the trait.
  4. Changes of the state definition of a Trait class will update the state definition of all its clients.

Source Code

The code for VWTraits can be found in the open repository in the packages "VWTraits" and "VWTraits Base VW Overrides".

Note: There is a new release available which incorporates other packages than what is listed above. See the "release" section below for further details.

Usage

Traits are merged into client classes using a trait specification. The trait specification is a class pragma method that identifies the trait to be merged. Additionally, the specification has a list of excluded methods allowing the programmer to create special method versions.
myHairTrait
	<trait: 'MyTraits.RedHairTrait'
		excludeInstanceMethods: #()
		excludeClassMethods: #()>

Note: The Trait installer UI will now generate the pragma for the developer and is the preferred mechanism.

Conflict resolution

Conflict resolution is primarily the responsibility of the programmer. Methods with the same selector may be redefined if a trait with the same selector is specified. It is my opinion that because the programmer has the source code of all the classes in the image he is better equiped to resolve conficts than a complicated set of rules. However, if a trait class has an instance variable defined with the same name as one in a super class of a client class, the variable will not be added to the client class.

The design of VWTraits incorporates the use of policy objects to determine what action to take when particular situations are detected. Additional policies could be defined to assist in resolving conflicts.

Issues

Extensions

I have released extensions to VWTraits which integrate it to StORE as well as provide some tool support for the "using" of Traits. Soon should be at the latest end of the month (3/05). For more info those interested can check out the Traits section at NYC Smalltalk.

Note: The new release is out. See below.

-Charles A. Monteiro

Current Release:

Events:

Current Tasks


Edit Rename Changes History Upload Download Back to Top