Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: what does the "<:" symbol mean?

Tags:

julia

What does this mean in function signatures, for example:

convert(::Type{T}, z::Complex) where {T<:Real}

like image 320
kilgoretrout Avatar asked Oct 26 '18 19:10

kilgoretrout


People also ask

What does the expression f mean in Julia?

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.

Are symbols in Julia the same as in scheme?

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.

What are some special expressions for calling functions in Julia?

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.

Can Unicode be used for function names in Julia?

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.


2 Answers

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).

like image 77
phipsgabler Avatar answered Sep 21 '22 08:09

phipsgabler


<:(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
like image 44
Saqib Shahzad Avatar answered Sep 22 '22 08:09

Saqib Shahzad