N9I - 2/5/2010
Written on Fri, 05 Feb 2010 19:46:58 +0000 - Last updated on Fri, 12 Feb 2010 20:22:24 +0000A journal entry in the development of the N9I programming language, dated February 5, 2010
Annotations for Casting Methods
Many object-oriented languages include methods such as a "to string" method to conveniently "convert" an object to another type. N9I extends this by including a method annotation, @to, which indicates methods that can "automatically" convert objects to another type.
For example, the way to declare a to string method in N9I is the following:
#@to String
public String toString() ...This is not limited to strings, however. Imagine a vector class, Vector, that keeps a collection of items. This Vector is not an array, but a vector usually has a method named toArray or some such thing. To convert the vector to an array (that is, to create an array that is equivalent to the vector), one would thus do:
vector.toArray();
But suppose the vector's toArray method looked like this:
#@to Array
public Array toArray() ...Now one would use this vector anywhere one can use an array.
This can even be used with primitives
#@to int
public int toInteger() ...The methods are also called if you attempt to cast an object to a type:
(String) object // calls object.toString()Note that these methods return a representation of the actual object, not the object itself.
Followup
For a followup to this entry, which describes the reverse of the @to annotation - the @from annotation - see N9I - 2/12/2010