var i : Cplx = (0,1) echo(repr(i)) i.r = 3.4'f32
This produces a compilation on the last line, because I'm trying to store an int in a float. However, I declared Cplx to be of type (float,float). Why is my type declaration of float overridden by int?
The issue is that while ints are implicitly convertible to float (int, int) is not implicitly convertible to (float, float). While mathematically the Gaussian integers are a subring of the complex numbers, you have to tell the compiler how that embedding works. After all, as far as the compiler knows, you may be trying to represent Z[sqrt(-5)] instead with pairs of ints. Something like the following should work:
type
Complex = tuple[re, im: float]
converter toComplex(z: tuple[re, im: int]): Complex =
(z.re.float, z.im.float)
var i: Complex = (0, 1)
echo(repr(i))
var j : float64 = 1
j = 3.2
echo($j)
var i : Cplx = (0,1)
...to...
var i : Cplx = (0.0,1.0)
...then it compiles.
You were passing ints into a type that expects floats.
I think stevenrbrandt just misinterpreted the error message of the compiler and thought that the ints got assigned and the error arises because the "suddenly" int type gets assigned a float.
type
Cplx = tuple[r:float, i:float]
var i: Cplx = (0,1)
echo repr(i)
i.r = 3.4'f32
gives:
typemad.nim(3, 14) Error: type mismatch: got (tuple[int, int]) but expected 'Cplx'
stevenrbrandt The error is in line 3 there the compiler got your (0:int , 1:int) tuple and can't store it because he needs a Cplx aka (0:float, 1:float). The i never gets assigned the integer types. jehan showed you hot wo implement an automatic converter for int tuples to complex tuples.
P.S.: As he shows the "repr(i)" line and tells us that the type was suddenly int. Was there a compiler bug which made that possible int the past?