-
-
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
using ?.
and ?[ ]
for optional accessing [syntax]
#35699
Comments
Isn't this pretty much the same as julia> a = rand(5);
julia> get(a, 6, missing)
missing
julia> get(a, 4, missing)
0.03053521360980338
julia> d = Dict("foo" => "bar");
julia> get(d, "baz", missing)
missing
julia> get(d, "foo", missing)
"bar" |
For Arrays and Dicts yes. For structs, the method doesn't exist though. This is a simple extension to Things like |
That is true but you quite rarely access fields (properties) using variables in non-metaprogramming code.
Oh, so it should also implicitly broadcast? #19169 might be a bit relevant then. I think another proposal for |
This is very useful in certain applications like XML, HTML. For example, I might want to access a deep property. city?.university?.people?.name Writing this manually produces a mess: if hasfield(city, :university)
if hasfield(city.university, :people)
if hasfield(city.university.people, :name)
return city.university.people.name
else
return missing
end
else
return missing
end
else
return missing
end Things can be mixed too: city?[10]?.university?.people?["Tom"]?.age
I think that is the natural way. For example, for
That it is limited to one argument functions, and I don't quite like that. Instead, we can say x ?|> foo x === nothing ? missing : foo(x) That is quite in line with this issue. Since we are using The meaning for (I don't know if this is useful though) foo?(args...) could be: if isa(getfield(@__MODULE__, :foo), Function)
return foo(args...)
else
return missing # or something else
end Here we interpreted |
You could do something like this for that use case: julia> struct A
a
end
julia> struct MyMissing end
julia> const mymissing = MyMissing()
MyMissing()
julia> Base.getproperty(a::A, s::Symbol) = hasfield(A, s) ? getfield(a, s) : mymissing
julia> Base.getproperty(::MyMissing, s::Symbol) = mymissing
julia> a = A(1)
A(1)
julia> a.a
1
julia> a.b
MyMissing()
julia> a.b.c.d.e
MyMissing() But then you do need to use a custom missing type ( |
@ericphanson Thank you for the solution, however, this:
|
If this is to be done, then |
To be honest, I prefer to have a new type similar to # the new missing:
missing.foo == missing
missing[1] == missing
missing["a"] == missing This allows partial parsing of the sentence. For example, the following can be parsed partially because city?.university?.people?.name
# if city didn't have university, the end result will be missing, while still being parsed partially But if we don't have access propagation, we need to parse the whole thing in a chunk. Since TypeScript and JavaScript 2020 have a similar syntax, I wonder how they have implemented this. My other reasons for preferring missing like type:
|
The |
You define the x === nothing && hasproperty(x, :y) ? getproperty(x, :y) : nothing
x === nothing && haskey(x, y) ? getindex(x, y) : nothing That's also why there's no need to cram any weird behaviors into the |
Might want to return a |
True, that would be more general and allow handling the case where |
Seems pretty common to have |
That is true, but it seems likely that one would want |
👍 for using dict?[:x] = obj?.x would mean to
Similar trick can work for "flexible" object type with a variable set of properties. |
Just to be explicit, with the |
I want to propose a new syntax for optional accessing:
FooStruct?.bar
for optional accessing ofstruct
fields.If the field is not present in the struct, the result will be
missing
instead of an error. The return will beFoo.bar
ifbar
exists inFoo
(normal case).FooArray?[10]
for optional accessing ofArray
elements.If the length of the given number is more than the length of the Array (out of bounds), the result will be
missing
instead of an error and will beFoo[10]
in normal case.FooDict?["bar"]
for optional accessing ofDict
values.If the key does not exist in the
Dict
, the result will bemissing
instead of an error and will beFoo["bar"]
in normal case.The implementation is straight forward. For example, for the
Dict
:Instead of
missing
, we can returnnothing
too. I prefermissing
here.The text was updated successfully, but these errors were encountered: