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

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
主站蜘蛛池模板: 建昌县| 桐城市| 福安市| 怀安县| 广平县| 泰顺县| 南昌县| 正蓝旗| 兖州市| 天峻县| 克什克腾旗| 阳朔县| 贵州省| 大理市| 香格里拉县| 德州市| 西贡区| 辛集市| 宣武区| 利川市| 大渡口区| 乌拉特后旗| 日土县| 沭阳县| 丰原市| 陆河县| 宜良县| 通州区| 双城市| 漳平市| 曲靖市| 志丹县| 巴林右旗| 吉安市| 阳江市| 从江县| 墨竹工卡县| 花莲县| 唐山市| 长治县| 仲巴县|