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

Can't infer types of elements of named tuples under getindex #24441

Closed
andyferris opened this issue Nov 2, 2017 · 7 comments
Closed

Can't infer types of elements of named tuples under getindex #24441

andyferris opened this issue Nov 2, 2017 · 7 comments

Comments

@andyferris
Copy link
Member

andyferris commented Nov 2, 2017

I had hoped that indexing a named tuple (#22194) with a Symbol would be a type-inferrable operation. I'm guessing that a tfunc or whatever, like for Tuple, may be needed here.

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.7.0-DEV.2400 (2017-11-02 03:12 UTC)
 _/ |\__'_|_|_|\__'_|  |  ajf/empty/5fa163f (fork: 1 commits, 0 days)
|__/                   |  x86_64-linux-gnu

julia> nt = (a=1, b=2.0)
(a = 1, b = 2.0)

julia> f(x) = x[:a]
f (generic function with 1 method)

julia> @code_warntype f(nt)
Variables:
  x::NamedTuple{(:a, :b),Tuple{Int64,Float64}}

Body:
  begin
      return (Base.getfield)(x::NamedTuple{(:a, :b),Tuple{Int64,Float64}}, :a)::Union{Float64, Int64}
  end::Union{Float64, Int64}
@andyferris andyferris changed the title Can't infer types of values of named tuples under getindex Can't infer types of elements of named tuples under getindex Nov 2, 2017
@StefanKarpinski
Copy link
Sponsor Member

@vtjnash, I believe you've done some constant prop work that might soon help here?

@andyferris
Copy link
Member Author

andyferris commented Nov 2, 2017

That would be even better :) I changed the OP to wrap in a function to better actually reflect the problem.

@StefanKarpinski
Copy link
Sponsor Member

That's the only way this could work. Note that the nt.a form is type inferred:

julia> f(x) = x.a
f (generic function with 1 method)

julia> @code_warntype f(nt)
Variables:
  x::NamedTuple{(:a, :b),Tuple{Int64,Float64}}

Body:
  begin
      return (Core.getfield)(x::NamedTuple{(:a, :b),Tuple{Int64,Float64}}, :a)::Int64
  end::Int64

@bramtayl
Copy link
Contributor

bramtayl commented Nov 2, 2017

You can usually get around constant propagation issues by wrapping things in a type. So Val{:a}() or Field{:a}() ala #1974 (comment) could avoid this issue. One reason why having a nice syntax for Field{:a}() would be nice, like .a. Alternatively @pure annotations seem to help.

@andyferris
Copy link
Member Author

Yes, you can definitely work around this in a variety of ways - it's just that intuitively we can use getindex on a tuple so why not a named tuple?

@JeffBezanson
Copy link
Sponsor Member

Duplicate of #24011

@JeffBezanson JeffBezanson marked this as a duplicate of #24011 Nov 2, 2017
@JeffBezanson
Copy link
Sponsor Member

See PR #24362

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

4 participants