- Mastering Julia
- Malcolm Sherrington
- 228字
- 2021-07-16 13:42:40
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 --
- JavaScript:Functional Programming for JavaScript Developers
- C語(yǔ)言程序設(shè)計(jì)(第2版)
- 編寫(xiě)高質(zhì)量代碼:改善Python程序的91個(gè)建議
- 跟小海龜學(xué)Python
- Mastering Unity Shaders and Effects
- Haxe Game Development Essentials
- Python Data Analysis Cookbook
- Hands-On Full Stack Development with Go
- Windows Phone 7.5:Building Location-aware Applications
- CoffeeScript Application Development Cookbook
- Clean Code in C#
- Fastdata Processing with Spark
- Java Web開(kāi)發(fā)教程:基于Struts2+Hibernate+Spring
- Android開(kāi)發(fā)權(quán)威指南(第二版)
- Swift 2 Blueprints