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

use Typename for constructors with Int, Float32, etcetera #3007

Closed
stevengj opened this issue May 3, 2013 · 6 comments
Closed

use Typename for constructors with Int, Float32, etcetera #3007

stevengj opened this issue May 3, 2013 · 6 comments
Labels
decision A decision on this change is needed

Comments

@stevengj
Copy link
Member

stevengj commented May 3, 2013

The usual convention in Julia is that the constructor has the same name as the type, e.g. we construct an Array with Array(...) etcetera.

However, some of the bits types don't follow this rule: the constructor for Int is int(...), the constructor for Float64 is float64(...), etcetera. This is inconsistent, annoying, and confusing.

It is especially annoying because it means that a typealias for one of those types also does not have a constructor, e.g. there is no constructor for Cint. Of course, one could define cint(...) but that is annoying (not to mention polluting the namespace).

Right now, trying to define constructors named Float32 etcetera fails:

julia> import Core.Float32
julia> Float32(x) = float32(x)
ERROR: invalid method definition: not a generic function

I'm not sure if there is any deep technical reason for this limitation?

@andrioni
Copy link
Member

andrioni commented May 3, 2013

Curiously, Float64 works.

julia> type T
       end

julia> Float64(::T) = 1.0
# methods for generic function Float64
Float64(::T) at none:1

Edit: OK, this is very weird.

julia> Float64(x) = x
ERROR: error in method definition: function Core.Float64 must be explicitly imported to be extended

julia> import Core.Float64

julia> Float64(x) = x
ERROR: invalid method definition: not a generic function

julia> Float32(x) = x
# methods for generic function Float32
Float32(x) at none:1

@JeffBezanson
Copy link
Sponsor Member

In the first case, you've created a new function called Float64, not related to Core.Float64.

@JeffBezanson
Copy link
Sponsor Member

I agree it would be much better not to have these redundant names.
One concern was that a constructor such as Int(x) ought to return something of type Int, while int(x) also converts arrays to element type Int. I suppose there ought to be a general version of that, something like arrayconvert(Int, a).

@StefanKarpinski
Copy link
Sponsor Member

map?

@blakejohnson
Copy link
Contributor

What about, for instance, float()? In #2930 we used the fact that since this function was not a constructor for the type, we were free to convert a Complex{Int} to a Complex{Float64}.

@JeffBezanson
Copy link
Sponsor Member

Dup of #1470.

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

No branches or pull requests

5 participants