Primitive parametric types

Is it possible to specify the bit size of a primitive type as a parameter?
If I try:

primitive type MyType{B,T,D} <: Number B end

I get :

ERROR: invalid declaration of primitive type MyType

additionally, I would like to use the parameters T and D as a type and a constant respectively in methods that take MyType{B,T,D} as one of their arguments

No. And you wouldn’t really want to do that. You can have a primitive type that has a parameter; you cannot use the parameter to provide part of the primitive type’s specific definition. The parameter cannot be swallowed into the declaration of the type itself.

Actually, that is exactly what I wanted to do :slightly_smiling_face:
I don’t understand the problems involved with this.

If I could simplify the question. How about the following:

julia> primitive type MyType{T} <: Number sizeof(T)*8 end

julia> sizeof(MyType{UInt32})
24

julia> sizeof(UInt32)
4

This syntax does compile, however as you can see the number of bits is 24*8, and this is the same whatever T is.
Is there any way to declare primitive parametric types with varying sizes?

There is not because primitive types are used to generate the system level number types and if one could declare a primitive type, say Float64, with varying size … well that would lead to the possibility of Float64s with the precision of Float16s. (OK, I may be overstating this … essentially, though you should not conceptualize primitive types (parameterized or not) as if they were another sort of possibly parameterized struct.)

Two different phyla [if you happen to be a Bio person].

What is it that you intend to do with this – there may be a much better approach that gives the outcome you seek. Pretend you had that sort of type – how are you applying it and why – to what end?

@hros let me know … we’ll keep moving this forward.

you might use a parameterized struct and this package
https://github.com/vtjnash/ComputedFieldTypes.jl
(it does nothing with primitive types, but your struct field can be of a computed type).

2 Likes