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

Composite types

A composition type is a collection of named fields, grouped together and treated as a single entity; these are termed records and structures in some programming languages.

If the type can also have functions (methods) associated with them the resulting collection is termed an object and the languages which support them (Java, C++, Python, Ruby, and so on) as object-oriented.

In Julia, functions are not bundled up with the data structures they operate on. The choice of the method a function uses is termed dispatch. When the types of ALL of a function's arguments are considered when determining the method employed, this is termed multiple dispatch and Julia uses this rather than the single dispatch we associated with object methods. We will be considering the implication of multiple dispatch in detail in the next chapter.

Composite type details are defined with the type keyword, followed by a list of field names, optionally annotated with the :: operator and terminated with end. If the type of the field is not specified Any is assumed.

Consider a simple type definition for membership of a meetup group:

type Member
 fullname::ASCIIString
 email::ASCIIString
 meetup::ASCIIString
 age::Int
 organiser::Bool
 mobile::ASCIIString
end
me = ("Malcolm Sherrington", "malcolm@ljuug.org", "London Julia User Group", 55, true, "07777 555555")
julia> names(me)
6-element Array{Any,1}: # => [:fullname,:email,:group,:mobile,:organiser,:mobile]
julia>me.fullname #=> Malcolm Sherrington"
julia>me.mobile #=> "07777 555555" (-- not really my number --
主站蜘蛛池模板: 平湖市| 镇江市| 新营市| 西峡县| 永吉县| 左贡县| 巴林左旗| 和硕县| 开封县| 卢龙县| 宾阳县| 达拉特旗| 禄劝| 宁津县| 青河县| 邳州市| 年辖:市辖区| 黔江区| 崇文区| 和龙市| 简阳市| 珠海市| 潞西市| 湾仔区| 宁蒗| 东光县| 哈尔滨市| 忻州市| 泰兴市| 溧阳市| 开原市| 依安县| 寿宁县| 南漳县| 云梦县| 射阳县| 安陆市| 海门市| 保定市| 西贡区| 永川市|