What does this mean in function signatures, for example:
convert(::Type{T}, z::Complex) where {T<:Real}
Without parentheses, the expression f refers to the function object, and can be passed around like any other value: As with variables, Unicode can also be used for function names: Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that values are not copied when they are passed to functions.
My two bulleted questions still stand. Symbols in Julia are the same as in Lisp, Scheme or Ruby. However, the answers to those related questions are not really satisfactory, in my opinion.
A few special expressions correspond to calls to functions with non-obvious names. These are: [A B C ...] [A; B; C; ...] [A B; C D; ...] setindex! setproperty! Functions in Julia are first-class objects: they can be assigned to variables, and called using the standard function call syntax from the variable they have been assigned to.
As with variables, Unicode can also be used for function names: Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that values are not copied when they are passed to functions.
Strictly speaking, one should differentiate between the predicate Base.:(<:)
, as described in @Saqib's answer, and the syntactic usage of <:
for describing constraints.
This syntactic usage can occur in type parameter declarations of methods, to constrain a type variable to be a subtype of some other type:
f(x::T) where {T<:Real} = zero(x)
A sort of special case of this is when you constrain the type parameter of a struct (struct Foo{T<:Real} ... end
) -- that constrains the methods of the generated constructor, and allows the type constructor to be applied only to the constrained subtypes.
On the other hand, outside of type parameters, <:
can be used to declare a new type as a subtype of some other (necessarily abstract) type:
struct Foo <: Real end
Although both cases are in line with the meaning of the subtyping predicate, you can't replace them with other arbitrary expressions (e.g., you can't write ... where {isreal(T)}
in f
).
<:(T1, T2)
Subtype operator: returns true
if and only if all values of type T1
are
also of type T2
.
Examples:
Float64 <: AbstractFloat
=> true
Vector{Int} <: AbstractArray
=> true
Matrix{Float64} <: Matrix{AbstractFloat}
=> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With