-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
More explicit representation of undef-ability of fields #25519
Comments
Rust has this. They use it instead of mutable struct Never
allow::Never
let unconstructable end
end
convert(::Type{Union{Never, T}}, x) = convert(T, x) # and a few others, c.f. Some/Nothing
Not intentional. Will be fixed with my latest PR. |
Not sure what you're trying to show with that example. The point would be to throw an UndefRefError on field access. |
Yes, precisely. julia> mutable struct foo{T}
x::Union{Never, T}
foo{T}() where {T} = new{T}()
end
julia> foo{Int}().x
ERROR: UndefRefError: access to undefined reference
julia> foo{String}().x
ERROR: UndefRefError: access to undefined reference |
What about a struct Foo
baz::Vector{Int}
bar?::Int
end For this struct Foo
baz::Vector{Int}
bar::Union{Nothing,Int}=nothing
end Of course, this isn't going to land in time for 1.0. |
I'm more in favor of @StefanKarpinski's 2nd idea of providing a syntax like |
See also previous issue about using |
I was griping to @JeffBezanson that the ability of non-isbits fields to be undef (i.e. throw an UndefRefError) didn't seem very first-class, especially since the definition of what isbits is changing these days. I was suggesting that we might want to have an explicit way to annotate that a field may be undef until defined, independent of whether that field is isbits or not. Possible syntax (names as always subject to bikeshed):
The text was updated successfully, but these errors were encountered: