Stream: helpdesk (published)

Topic: Type parameter in function undefined?


view this post on Zulip Timothy (May 12 2023 at 15:54):

Might anyone be able to explain what's going on here?

julia> r = Ref{TypeVar(:I, Union{}, Integer)}
Ref{I<:Integer}

julia> tt(::Type{A}) where {A} = A
tt (generic function with 1 method)

julia> tt(r)
ERROR: UndefVarError: `A` not defined
Stacktrace:
 [1] tt(#unused#::Type{Ref{I<:Integer}})
   @ Main ./REPL[219]:1
 [2] top-level scope
   @ REPL[220]:1

view this post on Zulip Sukera (May 12 2023 at 16:00):

Interesting one - I don't think you should be able to define r like that

view this post on Zulip Sukera (May 12 2023 at 16:00):

julia> r = Ref{I<:Integer}
ERROR: UndefVarError: `I` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

view this post on Zulip Sukera (May 12 2023 at 16:01):

compared to

julia> r = Ref{I} where I <: Integer
Ref{I} where I<:Integer

julia> tt(::Type{A}) where {A} = A
tt (generic function with 1 method)

julia> tt(r)
Ref{I} where I<:Integer

view this post on Zulip Sukera (May 12 2023 at 16:01):

since TypeVar is mutable, it should not be possible to place it in that position - looks like a bug to me

view this post on Zulip Timothy (May 12 2023 at 16:15):

I find this curious on two levels:

  1. That this "looks like it works"
  2. The nature of the error message

view this post on Zulip Sukera (May 12 2023 at 16:16):

it's not a type that should be constructible, so it's unsurprising that the method says "this doesn't exist, what are you doooing"

view this post on Zulip Sukera (May 12 2023 at 16:17):

would be good to have an issue for this, I think

view this post on Zulip Timothy (May 12 2023 at 16:17):

I feel like it's more of a "is malformed" than "doesn't exist" situation

view this post on Zulip Sukera (May 12 2023 at 16:20):

mutable objects are forbidden from being a type paremeter - the type quite literally does not (or isn't supposed to) exist/be constructible

view this post on Zulip Timothy (May 12 2023 at 16:22):

I'm still surprised about TypeVar being mutable

view this post on Zulip Sukera (May 12 2023 at 16:23):

my guess is that this is so because it has to interface with C and writing that mutating then was/is easier in the compiler

view this post on Zulip Sukera (May 12 2023 at 20:04):

Can you open an issue for this? Since this shouldn't be constructible, this ought to error I think

view this post on Zulip Timothy (May 13 2023 at 05:33):

Done: https://github.com/JuliaLang/julia/issues/49799


Last updated: Nov 22 2024 at 04:41 UTC