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

More on Types, Methods, and Modules

Julia has a rich built-in type system, and most data types can be parameterized, such as Array{Float64, 2} or Dict{Symbol, Float64}. Typing a variable (or more exactly the value it is bound to) is optional. However, indicating the type of some variables, although they are not statically checked, can provide some of the advantages of static-type systems as in C++, Java, or C#. A Julia program can run without any indication of types, which can be useful in a prototyping stage, and it will still run fast. However, some type indications can increase the performance by allowing more specialized multiple dispatch. Type assertions also help the LLVM compiler to create more compact, better optimized code. Moreover, typing function parameters makes the code easier to read and understand. The robustness of the program is also enhanced by throwing exceptions, in cases where certain type operations are not allowed. These failures will manifest themselves during testing, or the code can provide an exception handling mechanism.

All functions in Julia are inherently generic or polymorphic, that is, they can operate on different types of their arguments. The most appropriate method (an implementation of the function where argument types are indicated) will be chosen at runtime to be executed, depending on the type of arguments passed to the function. As we will see in this chapter, you can also define your own types, and Julia provides a limited form of abstract types and subtyping.

A lot of these topics have already been discussed in previous chapters; for example, refer to the Generic functions and multiple dispatch section in Chapter 3, Functions. In this chapter, we broaden the previous discussions by covering the following topics:

  • Type annotations
  • The type hierarchy—subtypes and supertypes
  • Concrete and abstract types
  • User-defined and composite types
  • Types and collections—inner constructors
  • Type unions
  • Parametric types and methods
  • Standard modules and paths
主站蜘蛛池模板: 湘乡市| 莎车县| 克拉玛依市| 循化| 泗洪县| 平谷区| 峨边| 新营市| 大同市| 肥乡县| 宜阳县| 利津县| 阿巴嘎旗| 灌阳县| 神池县| 荔波县| 哈密市| 温宿县| 怀远县| 内乡县| 榆林市| 南平市| 襄垣县| 夹江县| 泰宁县| 于都县| 大埔区| 油尖旺区| 余干县| 保康县| 昌黎县| 莱芜市| 江阴市| 哈尔滨市| 犍为县| 新兴县| 于田县| 梓潼县| 铜梁县| 星座| 和林格尔县|