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

Inner constructor type constraint fails with UndefVarError in iteration #38888

Closed
david-pl opened this issue Dec 15, 2020 · 0 comments
Closed
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@david-pl
Copy link

Consider the following bit of code:

struct MyStruct{T}
    x::T
    function MyStruct(x::S) where S<:Int
        new{S}(x)
    end
end
n = 3
# MyStruct(n) # works if you uncomment this line
[MyStruct(n) for i=1:3]

This will fail with an UndefVarError: S not defined. Once this error is thrown, it is not possible to construct MyStruct anymore. However, when restarting Julia constructing an instance of MyStruct in global scope works. If you then execute the list comprehension it also works.
Note that the same happens if you use a function instead of the list comprehension, inside of which MyStruct(n) is called, so I guess it's related to scoping.

A simple workaround is to relax the type constraint, e.g. S<:Integer works as expected. Still, it's strange that the list comprehension works when MyStruct is constructed in global scope before.

Version info:

Julia Version 1.5.3
Commit 788b2c77c1 (2020-11-09 13:37 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
@JeffBezanson JeffBezanson self-assigned this Dec 15, 2020
@JeffBezanson JeffBezanson added the bug Indicates an unexpected problem or unintended behavior label Dec 15, 2020
KristofferC pushed a commit that referenced this issue Dec 19, 2020
staticfloat pushed a commit that referenced this issue Jan 15, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this issue May 4, 2021
staticfloat pushed a commit that referenced this issue Dec 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants