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

Should vararg keyword arguments be returned as Pair? #15737

Closed
eschnett opened this issue Apr 1, 2016 · 8 comments
Closed

Should vararg keyword arguments be returned as Pair? #15737

eschnett opened this issue Apr 1, 2016 · 8 comments
Assignees
Milestone

Comments

@eschnett
Copy link
Contributor

eschnett commented Apr 1, 2016

With

f(;args...)=args

there is

julia> f(x=1)
1-element Array{Any,1}:
 (:x,1)

julia> f(x=1) |> typeof
Array{Any,1}

julia> f(x=1)[1] |> typeof
Tuple{Symbol,Int64}

It might be nicer if the tuple (:x,1) was a Pair instead.

@stevengj
Copy link
Member

stevengj commented Apr 1, 2016

It would also be nicer if args were an Associative type (albeit with simple linear search, not a heavyweight Dict).

@eschnett
Copy link
Contributor Author

eschnett commented Apr 1, 2016

args is currently an Array{Any,1} -- would it make sense to use NTuple instead?

@eschnett
Copy link
Contributor Author

eschnett commented Apr 2, 2016

See https://github.com/eschnett/SimpleAssoc.jl for a package that shows how an AssocArray or AssocTuple could be implemented -- associative collections based on arrays or tuples.

@tkelman
Copy link
Contributor

tkelman commented Apr 2, 2016

There's already an ImmutableDict in base as an implementation detail of some recent output refactoring, I don't think it's exported though and maybe not documented either.

julia/base/dict.jl

Lines 935 to 950 in c45175f

"""
ImmutableDict
ImmutableDict is a Dictionary implemented as an immutable linked list,
which is optimal for small dictionaries that are constructed over many individual insertions
Note that it is not possible to remove a value, although it can be partially overridden and hidden
by inserting a new value with the same key
ImmutableDict(KV::Pair)
Create a new entry in the Immutable Dictionary for the key => value pair
- use `(key => value) in dict` to see if this particular combination is in the properties set
- use `get(dict, key, default)` to retrieve the most recent value for a particular key
"""

@eschnett
Copy link
Contributor Author

eschnett commented Apr 2, 2016

Hmm: There is

ImmutableDict{K,V}(KV::Pair{K,V}) = ImmutableDict{K,V}(KV[1], KV[2])

but:

julia> typealias D Base.ImmutableDict{Char,Int}
Base.ImmutableDict{Char,Int64}

julia> D('a'=>1)
ERROR: MethodError: Cannot `convert` an object of type Pair{Char,Int64} to an object of type Base.ImmutableDict{Char,Int64}
This may have arisen from a call to the constructor Base.ImmutableDict{Char,Int64}(...),
since type constructors fall back to convert methods.
Closest candidates are:
  convert{T}(::Type{T}, ::T)
  (::Type{BoundsError})(::ANY)
  (::Type{BoundsError})(::ANY, ::ANY)
  ...
 in Base.ImmutableDict{Char,Int64}(::Pair{Char,Int64}) at ./sysimg.jl:48
 in eval(::Module, ::Any) at /Users/eschnett/julia05nt/lib/julia/sys.dylib:-1

Apparently needs a few tests...

@vtjnash
Copy link
Sponsor Member

vtjnash commented Apr 2, 2016

that's an outer constructor, and works as intended:

julia> Base.ImmutableDict('a'=>1)
Base.ImmutableDict('a'=>1)

@eschnett
Copy link
Contributor Author

eschnett commented May 8, 2016

In https://github.com/eschnett/FlexibleArrays.jl , I used the names lbnd and ubnd for the lower and upper bounds, respectively. I like the idea of returning both in one go. This also allows strided arrays if one returns a StepRange. This even generalizes to sparse arrays, where one could return just the list of indices (or an iterator to them).

@JeffBezanson JeffBezanson added this to the 1.0 milestone Apr 30, 2017
@JeffBezanson JeffBezanson self-assigned this Apr 30, 2017
@JeffBezanson
Copy link
Sponsor Member

Dup of #4916.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants