Hi,
sorry if this is a newbie question.
My understanding is that int is both a type and a type class:
> Furthermore, every generic type automatically creates a type class of the
> same name that will match any instantiation of the generic type.
>
> int - the generic signed integer type; its size is platform dependent
Also,
> A type class can be used directly as the parameter's type. Procedures
> utilizing type classes in such manner are considered to be implicitly generic.
I was wondering if (on a 64bit platform)
proc xyz(n : int) = ...
Run
would be interpreted as
proc xyz(n : int64) = ...
Run
or
proc xyz[T : int](n: T) = ...
Run
On investigation, it appears that neither is correct. Instead,
* the int refers to specific type rather than a type class
* the type int is distinct from int64, even on a 64bit platform
* int64 is not implicitely convertible to int
* the type class int does not include the type int64 , i.e.
let n : int64 = 1
proc test[T : int](n : T) = ...
test(n)
Run
will not work. To create a generic procedure for all int types one has to
use
proc test[T : int64](n : T) = ...
Run
instead.
* In the above example, ìnt64 defines a type class that includes all
implicitly convertible int types, and similarly for the other int types. As a
result
proc test[T : int64](n : T) = ...
proc test[T : int16] (n : T) = ...
Run
will fail with an "Error: ambiguous call"
* However, if we use the int64 in an expression, int64 will refer to a single
type again, i.e.
proc test[T : int64 | int8](n : T) = ...
proc test[T : int16 | int32] (n : T) = ...
Run
works as expected.
* Unlike int and int64, float and float64 are the same type, i.e.
proc test(x : float64) = ...
proc test(x : float) = ...
Run
will fail with an "Error: redefinition of 'test'; ...".
Is my understanding correct? If so, what is the reasoning for the design
choices that result (to my mind) counter-intuitive program behaviour?
Thanks for your help.