Writing a convert function with parametric types


#1

I’m trying to write a custom convert for FixedPointNumber and finding that I’m still on a learning curve.

The goal is to be able to write

convert(Fixed{Int16,12}, Fixed{Int16, 10}(0.2231))

And have it do the conversion.
I tried borrowing something from the normed library,

convert(::Type{Fixed{T1,f}}, x::Fixed{T1,f}) where {T1 <: Signed,f} = Fixed{T1,f1}(convert(T1, x.i), 0)

And then realized that ,after trying it, that it won’t work because it’s got the same type for x and the type argument.
No problem I’ll just make the fractional identifier for each type for each unique and everything will be fine:

convert(::Type{Fixed{T1,f1}}, x::Fixed{T1,f2}) where {T1 <: Signed,f1} = Fixed{T1,f1}(convert(T1, x.i), 0)

I’ve maintained T1 to be the same for the conversion type and the argument because I don’t want to deal with converting Fixed{Int16,10} to Fixed{Int32,10} yet…

but then I get

ERROR: LoadError: LoadError: UndefVarError: f2 not defined

oops. At this point I’ve realized that I don’t actually know what I’m doing and I’m just guessing.
Could someone explain what’s actually going on in that statement ?


#2

An f2 type variable seems to be missing from the where clause.