'!' denotes a non-null type. It si the dual of '?', which denotes a option type.
'!' is only required to specify that a type variable is ranging over non-null types. For type constants like String, File, it is the default. So String = !String.
In this case, the declaration says that toString can be called on any value except null.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just drop the first line. Since toString is already defined, you don't need to do it again.
You need to *implement* it, which is what the last two lines do.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You don't need to declare toString, it is declared in nice.lang.
If you mean "implement the method", then it makes sense to do it in the same package as the class definitions. Which means in the same dir, but it can be in a different file.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
An implementation is found in any program that imports the module containing that implementation.
Concretely, the code for a method is created at then end of the compilation when all the packages are known. All the implementations are put together with the code that selects an implementation given the type of the arguments.
It is always the most specific implementation that will be chosen.
Missing implementations ambiguities are discovered at compile time.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, if you ask if the runtime type is used: yes. So yes, that should be how it goes.
Now the collection framework has not been worked on much yet, and toString is not implemented on collections.
You could build your own Nil and Cons, and implement toString on it, and it would work.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
eg
<Any T> String toString(!T)
'!' denotes a non-null type. It si the dual of '?', which denotes a option type.
'!' is only required to specify that a type variable is ranging over non-null types. For type constants like String, File, it is the default. So String = !String.
In this case, the declaration says that toString can be called on any value except null.
Ok, thanx.
And how do I overload toString?
[code]
public String toString(CType t);
toString(t@PolymorphicType) = "poly("+t.getBoundedVar()+", "+t.getBoundedType()+")";
toString(t@CType) = "undefined";
[/code]
This gives a abiguity error because toString is defined in nice.lang also.
Just drop the first line. Since toString is already defined, you don't need to do it again.
You need to *implement* it, which is what the last two lines do.
I have declared toString in another dir then the types. How will my function be found?
You don't need to declare toString, it is declared in nice.lang.
If you mean "implement the method", then it makes sense to do it in the same package as the class definitions. Which means in the same dir, but it can be in a different file.
But how is the implementation found? And what happens if there are more implementations of toString (that are ambiguous)? Which one will be chosen?
An implementation is found in any program that imports the module containing that implementation.
Concretely, the code for a method is created at then end of the compilation when all the packages are known. All the implementations are put together with the code that selects an implementation given the type of the arguments.
It is always the most specific implementation that will be chosen.
Missing implementations ambiguities are discovered at compile time.
But if I give a CType to eg a List en that list is going to be printed, will my toString be used, or the default implementation?
Well, if you ask if the runtime type is used: yes. So yes, that should be how it goes.
Now the collection framework has not been worked on much yet, and toString is not implemented on collections.
You could build your own Nil and Cons, and implement toString on it, and it would work.