Julia Concrete and Abstract Types
Concrete and abstract types are the two kinds of types in Julia. A concrete type can have values, and an abstract type cannot.
Only concrete types can have values
Int64 can have values. Number cannot.
println(isconcretetype(Int64))
println(isconcretetype(Number))Abstract types group related types
Number is abstract. Int64 is not abstract.
println(isabstracttype(Number))
println(isabstracttype(Int64))Every value has a concrete type
The value 5 has a concrete type. That type is Int64.
x = 5
println(typeof(x))
println(isconcretetype(typeof(x)))Example
abstract type Shape end
struct Circle <: Shape
r::Float64
end
c = Circle(2.5)
println(typeof(c))
println(isconcretetype(typeof(c)))
println(isabstracttype(Shape))
println(c isa Shape)Key points
- Concrete type: a concrete type can have values.
- Abstract type: an abstract type groups related types but has no values of its own.
- Value check: every value has one concrete type.
- Type test: isconcretetype tells whether a type is concrete.
- Abstract test: isabstracttype tells whether a type is abstract.
In this example, Shape is an abstract type and Circle is a concrete type under it. A circle value is made, its type is Circle, that type is concrete, Shape is abstract, and the value belongs to Shape.
Note
Int64 is concrete, and Number is abstract.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
Which of these is a concrete type?