-
Notifications
You must be signed in to change notification settings - Fork 32
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
Default values that are type stable #32
Comments
I don't understand how that would work. Note that this approach is going to have issues with type parameters because keyword arguments do not dispatch. |
If you tell it what
|
But the point stands that it's not type-stable: JuliaLang/julia#10146 (comment) But that's likely just JuliaLang/julia#16580, which isn't anything SimpleTraits.jl can do about. |
Now I understand the issue better. It makes sense that it doesn't work if we don't pass any argument for the type to be inferred. |
@ChrisRackauckas: yes. If you need type-stability you're out of luck with kwargs but you can always use the positional constructor. We probably need to wait for JuliaLang/julia#22194 . @juliohm: is there anything left to do here? If not, I'll close this. |
Some background: This is one of the trickier parts of this package. There are use cases as above, where specifying the type-parameters is what is needed/wanted. Then there are cases where we'd like the type-parameters to be inferred from the keyword arguments. Example: @with_kw immutable Bar1{T <: Real}
a::T=5
b::T
end is expanded into: immutable Bar1{T <: Real}
a::T # =5
b::T # no default
(::Type{Bar1{T}}){T}(; a=5,b=error("Field 'b' has no default.")) = Bar1{T}(a,b)
(::Type{Bar1{T}}){T}(a,b) = new{T}(a,b)
end
Bar1{T <: Real}(a::T,b::T) = Bar1{T}(a,b)
Bar1(; a=5,b=error("Field '" * "b" * "' has no default, supply it with keyword.")) = Bar1(a,b)
# This goes through the outer kw-constructor, which in terms goes
# through the outer positional (which figures out the type-parameter)
# then finally the inner positional:
Bar1(a=1,b=2)
# This goes through the inner kw-constructor, which in terms goes
# through the inner positional constructor
Bar1{Float64}(a=1,b=2) |
Thank you @mauro3 , closing this one. Got busy with other things, sorry for the delay in getting back to you. |
No worries and thanks for the feedback! |
The following example doesn't work:
it says that
T
is undefined. I can just use the literal1
, but that triggers a conversion behind the scenes, correct?Also, I'd love to see this functionality as part of the language with additional syntax. Is there an issue tracking this feature request yet?
The text was updated successfully, but these errors were encountered: