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

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
主站蜘蛛池模板: 湖南省| 额尔古纳市| 衡南县| 息烽县| 桓台县| 青龙| 邵东县| 贵德县| 彭州市| 西安市| 阿拉尔市| 沛县| 靖安县| 广饶县| 宿迁市| 义乌市| 嘉善县| 临海市| 家居| 赤城县| 民勤县| 弥勒县| 忻城县| 长宁区| 武强县| 梧州市| 察哈| 乐昌市| 漳平市| 广水市| 灯塔市| 台中市| 禹城市| 始兴县| 郧西县| 宣化县| 凤冈县| 梁山县| 桃园县| 徐闻县| 东源县|