-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: Type constructors as type parameters #15791
Comments
this assumes that a type parameter is the eltype of that type, which is false. |
FWIW, julia> immutable X{C}
end
julia> X{Vector}()
X{Array{T,1}}() The usage of the type parameter is the problem... |
Yes, although it may seem surprising, I believe supporting this is nearly impossible. For example you could write
which wouldn't terminate. |
@vtjnash I don't understand your comment. What does |
based on your examples, it seemed you were assuming that in for example, I could define a type |
@JeffBezanson Recursion could be addressed by disallowing recursion, or by imposing a recursion limit. My goal is to define a collection where the actual container type used in the implementation is not fixed. If the above construct is not possible, then I cannot write immutable X{C, T}
x::C{T}
end but I have to write immutable X{CT}
x::CT
end instead. This means that I can't define a type where |
@vtjnash Yes, type type "won't work" for some values of |
You don't need |
please reread my counter-example |
@vtjnash I understand your example -- it does not apply. |
@yuyichao Wouldn't this be expensive -- constructing a type |
Constructing a type cost nothing if type inference can infer the type. This is what |
julia> immutable X{C,T,CT}
x::CT
X() = new()
end
julia> Base.@pure _apply_type(C, T) = C{T}
_apply_type (generic function with 1 method)
julia> (::Type{X{C,T}}){C,T}() = X{C,T,_apply_type(C, T)}()
julia> X{Set,Int}()
X{Set{T},Int64,Set{Int64}}(#undef)
julia> @code_warntype X{Set,Int}()
Variables:
#self#::Type{X{Set{T},Int64,CT}}
Body:
begin # none, line 1:
GenSym(1) = $(Expr(:static_parameter, 1))
GenSym(0) = $(Expr(:static_parameter, 2))
$(Expr(:static_parameter, 1))
$(Expr(:static_parameter, 2))
return $(Expr(:new, X{Set{T},Int64,Set{Int64}}))
end::X{Set{T},Int64,Set{Int64}} |
This is a dupe of #8322 (third example). |
Actually type inference is smart enough without the julia> immutable X{C,T,CT}
x::CT
X() = new()
end
julia> (::Type{X{C,T}}){C,T}() = X{C,T,C{T}}()
julia> @code_warntype X{Set,Int}()
Variables:
#self#::Type{X{Set{T},Int64,CT}}
Body:
begin # none, line 1:
GenSym(1) = $(Expr(:static_parameter, 1))
GenSym(0) = $(Expr(:static_parameter, 2))
$(Expr(:static_parameter, 1))
return $(Expr(:new, X{Set{T},Int64,Set{Int64}}))
end::X{Set{T},Int64,Set{Int64}} |
Doesn't this require me to declare all functions |
yeah, |
No, it can just use the type.
AFAICT this is mainly a matter of how to generate |
It's for non-trivial compile time code generation when the compile time and type unstable input are not concerns. Doesn't help with this example with |
I still can't use this with typealias MyVector Vector{X(Set,Int)} doesn't work since I can't call a function in |
Not true. It's just a const assignment. julia> typealias MyVector Vector{(()->Int)()}
Array{Int64,1}
Mostly about using functions instead of For dispatch, you don't need to specify all the type parameters, you can just assume the type is legal. |
@yuyichao D'oh, my bad. Thanks for the correction. |
@yuyichao I like your solution; that's clever. Currently we require field types to be, well, types. To represent So, we could address this by instead lifting the requirement that field types be types. For example, field types could be thunks that get executed when type parameters are known. However this would make it difficult to get partial type information from field types. @yuyichao 's approach can be seen as implementing something like this manually via the constructor. |
This would be cool to have:
In this way, I can pass
Vector
orSet
orNTuple{4}
as type parameter toX
.The text was updated successfully, but these errors were encountered: