Skip to content
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

Closed
juliohm opened this issue Jun 27, 2017 · 8 comments
Closed

Default values that are type stable #32

juliohm opened this issue Jun 27, 2017 · 8 comments

Comments

@juliohm
Copy link

juliohm commented Jun 27, 2017

The following example doesn't work:

immutable Foo{T<:Real}
  a::T = one(T)
end

it says that T is undefined. I can just use the literal 1, 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?

@ChrisRackauckas
Copy link
Collaborator

ChrisRackauckas commented Jun 27, 2017

I don't understand how that would work. T isn't defined anywhere. How is it supposed to know what T should be? You need to say "the default T is ...", which is the same thing as writing a::T = 1.0.

Note that this approach is going to have issues with type parameters because keyword arguments do not dispatch.

@mauro3
Copy link
Owner

mauro3 commented Jun 27, 2017

If you tell it what T is it works:

julia> @with_kw immutable Foo{T<:Real}
         a::T = one(T)
       end                                                                                                                             
Foo{T<:Real}                                                                                                                           

julia> Foo{Float64}()                                                                                                                  
Foo{Float64}                                                                                                                           
  a: Float64 1.0                                                                                                                       

@ChrisRackauckas
Copy link
Collaborator

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.

@juliohm
Copy link
Author

juliohm commented Jun 27, 2017

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.

@juliohm juliohm closed this as completed Jun 27, 2017
@juliohm juliohm reopened this Jun 27, 2017
@mauro3
Copy link
Owner

mauro3 commented Jun 27, 2017

@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.

@mauro3
Copy link
Owner

mauro3 commented Jun 27, 2017

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)

@juliohm
Copy link
Author

juliohm commented Jun 28, 2017

Thank you @mauro3 , closing this one. Got busy with other things, sorry for the delay in getting back to you.

@juliohm juliohm closed this as completed Jun 28, 2017
@mauro3
Copy link
Owner

mauro3 commented Jun 28, 2017

No worries and thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants