Le mardi 08 mars 2016 à 04:12 -0800, Daniel Carrera a écrit :
> Hello,
>
> In Julia 0.4 the "int(foo)" syntax was deprecated:
>
> julia> int(3.2)
> WARNING: int(x::AbstractFloat) is deprecated, use round(Int,x)
> instead.
> ...
> 3
>
> I am happy with `round(Int,x)` but recently I noticed that Python
> consistently uses the name of the a type as the name of the function
> to convert values into that type. Compare:
>
> Python:
>
> list("hello") # Creates a `list` type.
> str(10) # Creates a `str` type.
> int(3.2) # Creates an `int` type.
> set("hello") # Creates a `set` type.
>
>
> Julia:
>
> collect("hello") # Creates an Array
> string(10) # Creates an ASCIIString
> round(Int,3.2) # Creates an Int
> Set("hello") # Creates a Set.
>
> I think the Python guys are onto something. It is easy to remember
> that the name of the function is the name of the type. Do you think
> there is any merit in Julia copying that idea? In Julia the
> equivalent might be something like this:
>
> Array("hello")
This case is tricky since Array{Int}(1) creates a vector with one
element, not an array containing 1. So for consistency we have to raise
an error for non-integer arguments.
> String(10)
String isn't a concrete type currently in Julia, that's the old name
for AbstractString. But the plans is to move to a single string type,
so this could work. I agree that it would be more logical than writing
string() in small case as currently.
> Int(3.2)
As Tim noted, this has to fail for correctness (and to protect
ourselves from potentially dangerous bugs).
> Set("hello")
>
> Currently only the last one works. The others give errors, and String
> is in fact deprecated. We could try the lower case versions:
>
> array("hello")
> string(10)
> int(3.2)
> set("hello")
>
> Now string(10) works, but int(3.2) is deprecated. The others don't
> exist but could exist:
>
> set(x) = Set(x)
> array(x) = collect(x)
Lower-case functions have been deprecated as much as possible. See
above about string vs. String. so I don't think we're going to add new
ones.
Regards
> I think it would be nice for Julia add this extra bit of consistency
> in the language. What do you think?
>
> Cheers,
> Daniel.