Type functions in structs

Is there a way to implement the following without error:

B2I(T) = T == Bool ? Int : T

struct S{T}
  x::B2I(T)
end

s1 = S{Bool}( 3 )

Cannot check if its fully correct right now but something like this should work I think

B2I(T) = T == Bool ? Int : T

struct S{T}
  x::T
  function S{T} (x) where T
     new{B2I(T)}(x)
end

s1 = S{Bool}( 3 )

In essence, you just use a normal definition of the struct with a new constructor that makes sure the type is converted correctly.

1 Like

That works. However, the point was to try to retain the fact that we want to view this as a Bool. I suppose I could store the type in a member but that feels inefficient.

Ah, would it be fine if your struct has two type parameters, (the first being Book). Otherwise, I’m not sure if it’s possible.
Afaik one has to store the type of the variables in the parametric struct at least (otherwise the function B2I would have to be called each time something is inferred I guess).