Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function of parameter type in type definition

Tags:

julia

Assume I want to store I vector together with its norm. I expected the corresponding type definition to be straightforward:

immutable VectorWithNorm1{Vec <: AbstractVector}
    vec::Vec
    norm::eltype(Vec)
end

However, this doesn't work as intended:

julia> fieldtype(VectorWithNorm1{Vector{Float64}},:norm)
Any

It seems I have to do

immutable VectorWithNorm2{Vec <: AbstractVector, Eltype}
    vec::Vec
    norm::Eltype
end

and rely on the user to not abuse the Eltype parameter. Is this correct?

PS: This is just a made-up example to illustrate the problem. It is not the actual problem I'm facing.

like image 339
gTcV Avatar asked Dec 11 '25 06:12

gTcV


1 Answers

Any calculations on a type parameter currently do not work (although I did discuss the issue with Jeff Bezanson at JuliaCon, and he seemed amenable to fixing it). The problem currently is that the expression for the type of norm gets evaluated when the parameterized type is defined, and gets called with a TypeVar, but it is not yet bound to a value, which is what you really need it to be called with, at the time that that parameter is actually bound to create a concrete type.

I've run into this a lot, where I want to do some calculation on the number of bits of a floating point type, i.e. to calculate and use the number of UInts needed to store a fp value of a particular precision, and use an NTuple{N,UInt} to hold the mantissa.

like image 164
Scott Jones Avatar answered Dec 12 '25 22:12

Scott Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!