- 18.11 (released) ...
Constructors and Creation Procedures
- Eiffel Creation Procedures
- Constructors in .NET
This section deals with what happens when objects, that is runtime instances of types, get created and initialized. When a new instance is created, there is an opportunity to initialize the state of the instance. This is done with a constructor in .NET, and with a creation procedure in Eiffel.
Eiffel Creation Procedures
Eiffel creation procedures are features of a class which can be used to initialize instances. Classes can have more than one creation procedure available. However, each creation procedure must ensure that the class invariant holds when the procedure completes execution. In other words, the creation procedure is there to initialize a newly created instance, and the class invariant guarantees that a newly initialized instance is actually valid.
There is nothing special about creation procedures themselves, they are just ordinary procedures (although by convention their names usually begin with the word "
make"). What makes them creation procedures is the fact that their names are listed as creation procedures in the class text.
In Eiffel, a creation procedure can be applied to an instance at any time (not just at object creation). This is done sometimes to reinitialize existing instances.
Constructors in .NET
Like creation procedures in Eiffel, .NET constructors are used to initialize new instances of types. Constructors manifest themselves differently depending upon which .NET language you use. In C#, constructors always appear as a method having the same name as the class on which they are implemented. In Visual Basic .NET, they always appear as a Sub with the name
New. Once compiled into an assembly, the metadata labels constructors as
Constructors can have multiple versions by overloading. That is, each version would have a different set of argument types.
Constructors can only be applied when a new instance is created.
Constructors as Eiffel Creation Procedures
When types from .NET assemblies are made available to Eiffel systems, the constructors are presented as creation procedures. Just as constructors show up in the C# and VB.NET environments with names appropriate to those languages, so it is with Eiffel for .NET. Always, constructors will have feature names which begin with the word "
make", the convention for creation procedure naming in Eiffel.
If there is only one version of the constructor, that version will be mapped to a single feature named
make. However, if there are overloaded versions of the constructor, then these versions given names starting with "
make_from_" and then followed with the argument names from the assembly metadata separated with the conjunction "
_and_". Let's look at an example.
The .NET type
System.Drawing.Sizehas an overloaded constructor with two versions. In the
System.Drawing assembly metadata, these two constructor versions look like this:
void .ctor(int32 width, int32 height)
void .ctor(System.Drawing.Point pt)
So the argument names for the first version are
height. For the second version there is only one argument named
pt. The constructor versions as presented to Eiffel programmers as creation procedures look like this:
make_from_width_and_height (width: INTEGER; height: INTEGER)
make_from_pt (pt: DRAWING_POINT)
Presenting the names in this format handles the conflicts of overloading and provides reasonably intuitive names that comply with Eiffel naming conventions.
Eiffel Creation Procedures as Constructors?
Eiffel creation procedures do not map to constructors when Eiffel classes are compiled into assemblies. Rather, they are actually manifested as functions on a factory class in the namespace
Create in the assembly. These functions return an initialized instance. In the section Type Organization there is more information about the organization of the types in assemblies built with Eiffel for .NET, along with an example of using types from such an assembly.