We already have some short array syntax, like Array{Int, 1} == Vector{Int}
Also, constructor short form: Int[]
(but not a type declaration)
Are there are any short forms for complex types declarations in function signatures and structure fields?
A couple of proposals:
We can use constructor semantics with special type-symbol ::
in front of it to declare some types:
- Arrays:
a = [1,2,3]
typeof(a)
>> Array{Int64,1}
# we can change this:
a::Array{Int64,1} # or Vector{Int64}
# into this, :: symbol prevents us from interpreting it like an array initializer
a::Int[] # variable-sized array
# also, for fixed-sized arrays (like MArray from StaticArrays.jl) we can add:
a::Int[4] # this is 4-element array with constant size
- Tuples:
t = (1, 3.)
typeof(t)
>> Tuple{Int64,Float64}
# we can change this:
t::Tuple{Int64,Float64}
# into this, :: symbol prevents us from interpreting it like a tuple of datatypes
t::(Int64, Float64)
- Named tuples:
nt = (pos = 1, val = 3.)
typeof(nt)
>> NamedTuple{(:pos, :val),Tuple{Int64,Float64}}
# that's a pretty long string to use, we can change this:
nt::NamedTuple{(:pos, :val),Tuple{Int64,Float64}}
# into this:
nt::NamedTuple{pos::Int64, val::Float64}
# of even shorter:
nt::(pos::Int64, val::Float64)
We can hide some default type names for composable types:
- Unions:
u::Union{T1, T2}
# can be shortened to:
u::{T1, T2}
- Unions with Missing, link to the original proposal
u::Union{T,Missing}
# replace with
u::T?
u::Union{T1, T2, Missing}
# replace with
u::{T1, T2}?
There are some additional options from Pair, Dict and Array constructors I’m not sure how to use, like:
x::[T1, T2]
x::(T1=>T2)
and so on.
Any other ideas?