Ok here is my code i get a stack overflow on the overloaded - operator with a distinct type.
import math
type F_TYPE* = distinct float32 # change to suit your needs
type BASE_TYPE = distinct float
type
Mat4_t* = array[16,F_TYPE]
type
Mat3_t* = array[16,F_TYPE]
type
Vec4_t* = ref object of RootObj
x*,y*,z*,w* : F_TYPE
type
Vec3_t* = ref object of RootObj
x*,y*,z* : F_TYPE
proc `/`*(a,b:F_TYPE): F_TYPE =
result = a / b
proc `==`(a,b:F_TYPE): bool =
if a == b:
return true
else:
return false
proc `-`*(a,:F_TYPE): F_TYPE =
return -a
proc `-`*(a,b:F_TYPE): F_TYPE =
result = (a - b)
And here is where the compiler stops when running the program.
proc mat4_frustum(left,right,bottom,top,near,far:F_TYPE): Mat4_t =
var dest : Mat4_t
var
rl = (right - -left).F_TYPE #code breaks here
tb = (top - -bottom).F_TYPE #and here
fn = (far - near).F_TYPE
dest[0] = (near.float * 2.0.float).F_TYPE / rl.F_TYPE
dest[5] = (near.float * 2.0.float).F_TYPE / tb.F_TYPE
dest[8] = (right + left).F_TYPE / rl.F_TYPE
dest[9] = (top + bottom).F_TYPE / tb.F_TYPE
dest[10] = - ((far + near).F_TYPE / fn).F_TYPE
dest[11] = - 1.0.F_TYPE
dest[14] = - ((far.float * near.float * 2).F_TYPE / fn.F_TYPE).F_TYPE
dest[15] = 1.0.F_TYPE
return dest
Specifically it breaks on the negative distinct type i think? Is this normal? I doubt it could be, because that would be not useful. So any ideas about how to get it to run correctly? or whatever else is needed...
The overflow is because of an infinite loop (and compiler is explicit about that if compiling in debug mode, without -d:release). Your proc - does nothing, except of calling itself with the same argument. It doesn't call the - of float32, as you probably expect - the arguments type is still F_TYPE. So just say to compiler to call the - of float32, by calling it for a float32 value:
proc `-`*(a:F_TYPE): F_TYPE =
return (-a.float32).F_TYPE
The same for two other procs.
Or you can do simpler - just borrow parent type's procs:
proc `-`*(a:F_TYPE): F_TYPE {.borrow.}