官术网_书友最值得收藏!

The type hierarchy – subtypes and supertypes

In Julia, every value has a type, for example, typeof(2) is Int64 (or Int32 on 32-bit systems). Julia has a lot of built-in types, in fact, a whole hierarchy starting from the type Any at the top. Every type in this structure also has a type, namely, DataType, so it is very consistent. typeof(Any), typeof(Int64), typeof(Complex{Int64}), and typeof(DataType) all return DataType. So, types in Julia are also objects; all concrete types, except tuple types, which are a tuple of the types of its arguments, are of type DataType.

Follow along with the code in  type_hierarchy.jl .

This type hierarchy is like a tree; each type has one parent given by the supertype function:

  • supertype(Int64) returns Signed
  • supertype(Signed) returns Integer
  • supertype(Integer) returns Real
  • supertype(Real) returns Number
  • supertype(Number) returns Any
  • supertype(Any) returns Any

A type can have a lot of children or subtypes (a function from the InteractiveUtils package) as follows:

  • subtypes(Integer) form 3-element Array{Any,1}, which contains Bool, Signed, and Unsigned
  • subtypes(Signed) form 6-element Array{Any,1}, which contains BigInt, Int128, Int16, Int32, Int64, and Int8
  • subtypes(Int64) is 0-element Array{Any,1}, which has no subtypes

To indicate the subtype relationship, the operator < is used: Bool <: Integer and Bool <: Any returns true, while Bool <: Char is false. The following is a visualization of part of this type tree:

主站蜘蛛池模板: 马鞍山市| 恩施市| 凌源市| 理塘县| 德令哈市| 顺义区| 澎湖县| 称多县| 永州市| 阳信县| 铜陵市| 临城县| 普兰县| 泰兴市| 新乡县| 农安县| 攀枝花市| 高州市| 黄石市| 岗巴县| 龙海市| 尼玛县| 赤城县| 青冈县| 德保县| 梁河县| 孟连| 饶阳县| 东海县| 普格县| 萨迦县| 雷山县| 台江县| 新巴尔虎右旗| 桂东县| 太湖县| 七台河市| 吕梁市| 右玉县| 谢通门县| 武冈市|