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

Feature request: type annotation on variables in @unpack #25

Open
jishnub opened this issue May 22, 2023 · 1 comment
Open

Feature request: type annotation on variables in @unpack #25

jishnub opened this issue May 22, 2023 · 1 comment

Comments

@jishnub
Copy link

jishnub commented May 22, 2023

E.g.

julia> d1 = Dict(:a=>3, :b=>2.0)
Dict{Symbol, Real} with 2 entries:
  :a => 3
  :b => 2.0

julia> function f(d1)
           @unpack a = d1
           a
       end
f (generic function with 1 method)

julia> @code_typed f(d1)
CodeInfo(
1%1 = invoke Base.getindex(d1::Dict{Symbol, Real}, :a::Symbol)::Real
└──      return %1
) => Real

In this case, if I know that a must be an Int, I might want to specify this as a::Int in the @unpack, but the following doesn't work:

julia> function f(d1)
           @unpack a::Int = d1
           a
       end
f (generic function with 1 method)

julia> f(d1)
ERROR: KeyError: key :Int not found
Stacktrace:
 [1] getindex(h::Dict{Symbol, Real}, key::Symbol)
   @ Base ./dict.jl:484
 [2] unpack
   @ ~/.julia/packages/UnPack/EkESO/src/UnPack.jl:35 [inlined]
 [3] macro expansion
   @ ~/.julia/packages/UnPack/EkESO/src/UnPack.jl:101 [inlined]
 [4] f(d1::Dict{Symbol, Real})
   @ Main ./REPL[290]:2
 [5] top-level scope
   @ REPL[291]:1
@mauro3
Copy link
Owner

mauro3 commented Jun 20, 2023

Thanks! Yes that would be a good feature, PR welcome.

Work-arounds:

julia> function f(d1)
           local a::Int
           @unpack a = d1
           a
       end
f (generic function with 1 method)

julia> function f(d1)
           (;a::Int) = d1
           a
       end
f (generic function with 1 method)

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

2 participants