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

Feature Request: allow default values for unspecified type parameters #4859

Closed
kmsquire opened this issue Nov 19, 2013 · 12 comments
Closed

Feature Request: allow default values for unspecified type parameters #4859

kmsquire opened this issue Nov 19, 2013 · 12 comments
Labels
speculative Whether the change will be implemented is speculative

Comments

@kmsquire
Copy link
Member

In #4038, I updated Dict to have a third type parameter, specifying whether the Dict should be Ordered or Unordered[1]. This allows one to create an OrderedDict with

julia> Dict{String,Int,Ordered}()
Dict{String,Int64,Int64}()

However, in order to create a normal Dict, I also need to specify Unordered:

julia> Dict{String,Int,Unordered}()
Dict{String,Int64,Nothing}()

julia> Dict{String,Int}()
ERROR: type cannot be constructed

I'd like to be able to simply specify Dict{String,Int}(), so that existing code continues to work unchanged.

This would seem to require the ability to specify a default value for a type parameter, or have it default to Any. Possible?

CC: @JeffBezanson, @StefanKarpinski


[1] It's irrelevant here, but Ordered is a type alias for Int64, and Unordered is a type alias for Nothing.

@JeffBezanson
Copy link
Member

This is one reason some types (like Array) avoid curly braces. For example, one can define

Dict(K,V) = Dict{K,V,Unordered}()

and then you almost have what you want.

This is a difficult change, since typically type parameters can be left "unspecialized" by leaving them off (e.g. Array{Int} for arrays of Int of any dimension). It might make sense for types to be able to opt-out of that behavior and specify defaults instead, but I'm not sure.

@kmsquire
Copy link
Member Author

typically type parameters can be left "unspecialized" by leaving them off

This is only when used, e.g., in function signatures, right? Ideally, I'd like to be able to keep this behavior (so that someone could pass an ordered or unordered Dict to a function), but allow defaults to be specified only for type construction.

@JeffBezanson
Copy link
Member

Currently there is no distinction between type construction inside function signatures and anywhere else.

@StefanKarpinski
Copy link
Member

If Dict{K,V} were a type constructor in the same way that Dict is, then wouldn't that help the situation? In essence, this would make Dict{} mean the same thing as Dict does. Note that this is not the same thing as having defaults for type parameters, although that could be taken to mean that, e.g., Dict{K,V}() = Dict{K,V,Unordered}() should be supplied as a method by default, or something like that. But that's a separate issue.

@kmsquire
Copy link
Member Author

For construction, I can't leave a type unspecialized, can I?

julia> type TestType{A,B} end

julia> TestType{Int,Int}()
TestType{Int64,Int64}()

julia> TestType{Int}()
ERROR: type cannot be constructed

@kmsquire
Copy link
Member Author

I would be quite happy with being able to specify Dict{K,V}() = Dict{K,V,Unordered}(). That would amount to allowing users to explicitly create/call functions parameterized with types, right?

@JeffBezanson
Copy link
Member

It is possible to make a partially-specialized type like TestType{Int} a generic function, so that methods can be added to it. However this is quite weird, and causes a lot of unpleasantness. The simplest issue is that the syntax Dict{K,V}() = ... is taken and already means something else.
I guess this is another case where one wants to define apply{K,V}(::Type{Dict{K,V}}).

@StefanKarpinski
Copy link
Member

I was going to write something along the same lines, but you beat me to it.

@kmsquire
Copy link
Member Author

Dict{K,V}(...) is defined only for the base type, and for when the ... contains arrays or other things from which the values of K and V can be inferred, right?

For my modified Dict, then, Dict{K,V,O}() is defined for the base type, but there's no definition for Dict{K,V}() (with no passed parameters)... if this were possible, I wouldn't have opened this issue. ;-)

@kmsquire
Copy link
Member Author

But apply{K,V}(::Type{Dict{K,V}}) would be useful as well.

@kmsquire
Copy link
Member Author

The simplest issue is that the syntax Dict{K,V}() = ... is taken and already means something else.

The point I was trying to make is that I don't believe this is defined explicitly as such (with no parameters), but only as the default type constructor.

@JeffBezanson
Copy link
Member

#1470 will make it possible to define constructors for partially-specified types, which seems to be the best way to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

3 participants