Kwargs for getindex with ;

question

#1

Since this is a corner case of syntax, I am asking here before opening an issue.

Keyword args are allowed for getindex since #25631 was fixed. But including an explicit ; causes problems:

julia> VERSION
v"1.1.0-DEV.785"

julia> struct Foo end

julia> f = Foo()
Foo()

julia> Base.getindex(::Foo, args...; kwargs...) = (args, kwargs)

julia> f[1, bar = 4]
((1,), Base.Iterators.Pairs(:bar=>4))

julia> f[bar = 4]
((), Base.Iterators.Pairs(:bar=>4))

julia> f[1; bar = 4]
ERROR: syntax: misplaced assignment statement in "f[1; bar = 4]"

julia> f[; bar = 4]
ERROR: syntax: unexpected ";"

Is this a bug?


#2

Could be ambiguous with the [1; 2] syntax.

f[(1; b=2)]
>((2,), Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}())

#3

Unfortunately, yes. But the () workaround you suggested is fine, thanks!


#4

It doesn’t. It’s simply 2.


#5

I thought the kwargs result was a bit odd, but if you can make use of it… =) just watch it isn’t changed / fixed
It’s dropping the one because it looks like nested code

You might also be able to add a method to Base.typed_vcat(::Foo)

f[1;2;3]
> no method matching typed_vcat(::Foo, ::Int, ::Int, ::Int)

Base.typed_vcat(x::Foo, args, kwargs...) = getindex(x, args, nt = kwargs)

f[1; (c=2, d=3)]
> ((1,), Base.Iterators.Pairs(:nt => ((c=2, d=3),) ))

#6

Thanks, I overlooked this.

In any case, I can make the desired syntax work for my use case, which is

table[(:select, :these, :columns)]
table[drop = (:those, :columns)]