Julia: inconsistent error for `let` bound variables conflicting with static parameters

Created on 2 Sep 2019  ยท  4Comments  ยท  Source: JuliaLang/julia

EDIT:

julia> function f(x::T) where {C1, C2, T}
           let C1 = 1,
               C2 = 2
               (C1, C2, T)
           end
       end
f (generic function with 1 method)

julia> f(1)
ERROR: UndefVarError: C1 not defined
bug lowering

Most helpful comment

Oh wait, now I understand (sorry!): this is a bug report. Compare the following:

julia> function g(x::T) where {C1, C2, T}
           let C1 = 1, C2 = 2
               return (C1, C2, T)
           end
       end
g (generic function with 1 method)

julia> function h(x::T) where {C, T}
           let C = 1
               return (C, T)
           end
       end
h (generic function with 1 method)

julia> h(nothing)
(1, Nothing)

julia> g(nothing)
ERROR: UndefVarError: C1 not defined
Stacktrace:
 [1] g(::Nothing) at ./REPL[1]:3
 [2] top-level scope at REPL[4]:1

julia> @code_lowered h(nothing)
CodeInfo(
1 โ”€      C = 1
โ”‚   %2 = Core.tuple(C, $(Expr(:static_parameter, 2)))
โ””โ”€โ”€      return %2
)

julia> @code_lowered g(nothing)
CodeInfo(
1 โ”€      C1 = 1
โ”‚        C2 = 2
โ”‚   %3 = Core.tuple($(Expr(:static_parameter, 1)), C2, $(Expr(:static_parameter, 3)))
โ””โ”€โ”€      return %3
)

It's of course not how you should write Julia code, but the inconsistency is a bug.

All 4 comments

Am I understanding correctly that you're asking for a new feature in which static parameters can be supplied with default values? What is the use case for this? In general the static parameters should be determined by the input types.

@timholy No, it's okay to leave the non-dispatched static parameters undefined, i.e, I think this is expected:

julia> function f(x::T) where {C1, C2, T}
           C1
       end
f (generic function with 1 method)

julia> f(1)
ERROR: UndefVarError: C1 not defined

However, in my first post I used a let binding to enter a new scope, and bind C1 = 1.

Is this explanation clear to you?

Nope. If this is a feature request, you need to clarify why you want this. If you think it's a bug (it's not), please ask questions on discourse.

Oh wait, now I understand (sorry!): this is a bug report. Compare the following:

julia> function g(x::T) where {C1, C2, T}
           let C1 = 1, C2 = 2
               return (C1, C2, T)
           end
       end
g (generic function with 1 method)

julia> function h(x::T) where {C, T}
           let C = 1
               return (C, T)
           end
       end
h (generic function with 1 method)

julia> h(nothing)
(1, Nothing)

julia> g(nothing)
ERROR: UndefVarError: C1 not defined
Stacktrace:
 [1] g(::Nothing) at ./REPL[1]:3
 [2] top-level scope at REPL[4]:1

julia> @code_lowered h(nothing)
CodeInfo(
1 โ”€      C = 1
โ”‚   %2 = Core.tuple(C, $(Expr(:static_parameter, 2)))
โ””โ”€โ”€      return %2
)

julia> @code_lowered g(nothing)
CodeInfo(
1 โ”€      C1 = 1
โ”‚        C2 = 2
โ”‚   %3 = Core.tuple($(Expr(:static_parameter, 1)), C2, $(Expr(:static_parameter, 3)))
โ””โ”€โ”€      return %3
)

It's of course not how you should write Julia code, but the inconsistency is a bug.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ararslan picture ararslan  ยท  3Comments

sbromberger picture sbromberger  ยท  3Comments

Keno picture Keno  ยท  3Comments

felixrehren picture felixrehren  ยท  3Comments

arshpreetsingh picture arshpreetsingh  ยท  3Comments