genericity
You are here :

Sub nodes

Recent changes
Site plan
 
 

Every classes and methods can be made generic. Simply add the generic declaration at the end of the method or class name.

generic declaration's syntax :

 <Name [ implements TypeName ] [ = TypeName ]>

Example :

 < T implements Vehicule = Car >

Generic class example :

 public class Vector<T> {
   private T[] elements;
 }
 public class HashMap<Key implements Hashable, Value> {
 }

Once defined, the generic argument (here Key and Value) become real TypeName in the rest of the code. Additional checks are performed here :

When instantiating class HashMap, the typechecker will ensure that Key really inherits from Hashable.

Method class example :

 public class Vector<T>
 {
   public sub someOtherMethod<Y>(Y parameter) {
   }
 }

Here we inject another type Y in method someOtherMethod(). That is, several someOtherMethod() methods might exists...

A notes on generic types

Generic types are not just code that is adapted. All generic types really exists in the symbol table of the compiler. All generic types will give result to a real instantiated type that will be optimised independantly from the other generic types of the same family.

Generic types and the source path

Generic types can be partially implemented...

That is, you have the right to write :

 class Vector<T> {
 }
 class Vector<string> {
 }

in the same Nosica file. The file will have to be named Vector.nos. The first version is the general implementation, while the last one is a specialised implementation for string ...

The source path is a variable of your property file that defines the roots of your Nosica source hierarchy.

Example :

 source_path = /home/user/project/library1, /home/user/project/library2, /usr/local/nosica/stdlib

That is, when searching for a TypeName, the compiler will check if it can find it in those 3 directories.

It is perfectly legal to define several time in different source path the same Nosica class provided this class is generic.

That is, you can provide your own version of a specialisation of Vector<int> in your own source path if you want.

The generic class Vector is defined as net.nosica.container.Vector in the nosica standard library (NSL). If you want to provide a default implementation that deals with 'int' as in the above example, simply define a new class in /home/user/project/library1 for example. The complete path to your specialised Vector.nos file will be : /home/user/project/library1/net/nosica/container/Vector.nos

Propulsed by Wiclear