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

Request: Allow type aliases to be used as outer constructors #3427

Closed
kmsquire opened this issue Jun 17, 2013 · 5 comments
Closed

Request: Allow type aliases to be used as outer constructors #3427

kmsquire opened this issue Jun 17, 2013 · 5 comments

Comments

@kmsquire
Copy link
Member

I'd like to be able to use type aliases as outer constructors. My use case is in my latest incarnation of OrderedDicts, although the same applies (I think) to, e.g., Vector{T}.

In my case, I define a base type OrderedDictBase

abstract DictItem{K,V}

type OrderedDictBase{K,V,Item<:DictItem,Itr} <: Associative{K,V}
    ht::Dict{K,Item} # really: Item{K,V}
    order::Itr       # really: Itr{Item{K,V}}

    OrderedDictBase() = OrderedDictBase(K,V,Item,Itr)  # callback to outer constructor
    OrderedDictBase(ht::Dict{K,Item}, order::Itr) = new(ht, order)
end

and specific versions as

typealias OrderedDict{K,V} OrderedDictBase{K,V,LinkedDictItem,LinkedDictItem}
typealias ArrayOrderedDict{K,V} OrderedDictBase{K,V,ArrayDictItem,Array}

I'd like to be able to define OrderedDict() as an outer constructor,

OrderedDict() = OrderedDict(Any,Any)
OrderedDict(K::Type, V::Type) = OrderedDictBase{K,V,LinkedDictItem,LinkedDictItem}()

But when I do, I get

julia> using OrderedCollections
ERROR: invalid method definition: not a generic function
 in include_from_node1 at loading.jl:91 (repeats 2 times)
 in reload_path at loading.jl:114
 in require at loading.jl:48
at /home/kmsquire/.julia/v0.2/OrderedCollections/src/OrderedDict.jl:201
at /home/kmsquire/.julia/v0.2/OrderedCollections/src/OrderedCollections.jl:3

My current solution is to define the typealias as _OrderedDict{K,V} (https://github.com/kmsquire/OrderedCollections.jl/blob/simplified/src/OrderedDict.jl#L199).

This works, but is a little inelegant.

Can this be done?

Somewhat related to @amitmurthy's comment in #3214 (comment)

@JeffBezanson
Copy link
Sponsor Member

Apologies, but this will probably not happen.

@StefanKarpinski
Copy link
Sponsor Member

Can you comment on why it won't happen? I've often wanted this as well. Seems related to #1470.

@kmsquire
Copy link
Member Author

I was just going to ask the same thing.

@cbecker
Copy link
Contributor

cbecker commented Feb 21, 2014

I would also like to find out why, has been discussed here as well https://groups.google.com/forum/#!topic/julia-users/FO-xpNDrfoM

@JeffBezanson
Copy link
Sponsor Member

#1470 might make this possible.

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

4 participants