Reader small image

You're reading from  Hands-On Design Patterns and Best Practices with Julia

Product typeBook
Published inJan 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838648817
Edition1st Edition
Languages
Right arrow
Author (1)
Tom Kwong
Tom Kwong
author image
Tom Kwong

Tom Kwong, CFA, is an experienced software engineer with over 25 years of industry programming experience. He has spent the majority of his career in the financial services industry. His expertise includes software architecture, design, and the development of trading/risk systems. Since 2017, he has uncovered the Julia language and has worked on several open source packages, including SASLib.jl. He currently works at Western Asset Management Company, a prestige asset management company that specializes in fixed income investment services. He holds an MS degree in computer science from the University of California, Santa Barbara (from 1993), and he holds the Chartered Financial Analyst designation since 2009.
Read more about Tom Kwong

Right arrow

Inheritance and Variance

If we had to choose the most important thing to learn in Julia, or in any programming language, then it has to be the concept of the data type. Abstract types and concrete types work together, providing the programmer with a powerful tool to model solutions to solve real-world problems. Multiple dispatch rely on well-defined data types to invoke the right functions. Parametric types are used so that we can reuse the basic structure of an object with a specific physical representation of the underlying data. As you can see, having a well-thought-out design for data types is of the utmost importance in software engineering practice.

In Chapter 2, Modules, Packages, and Data Type Concepts, we learned about the basics of abstract and concrete types and how to build a type hierarchy based upon an inheritance relationship between the types. In Chapter 3...

Technical requirements

Implementing inheritance and behavior subtyping

When we learned about inheritance, we realized that abstract types can be used to describe real-world concepts. We can say quite confidently that we already know how to classify concepts with parent–child relationships. With this knowledge, we can build a type hierarchy around those concepts. For example, the personal asset type hierarchy from Chapter 2Modules, Packages, and Data Type Concepts, looks like the following:

All data types shown in the preceding diagram are abstract types. Going from the bottom up, we know that both House and Apartment are subtypes of Property, and we know that both Property and Investment are a subtype of Asset. These are all reasonable interpretations based on how we speak about these concepts in our daily life.

We also talked about concrete types that are the physical realization of...

Covariance, invariance, and contravariance

As it turns out, the rules for subtyping are not very straightforward. When you look at a simple type hierarchy, you can immediately tell whether one type is a subtype of another by tracing the relationships of the data types in the hierarchy. The situation becomes more complex when parametric types are involved. In this section, we will take a look at how Julia is designed with respect to variance, a concept that explains subtyping relationships for parametric types.

Let's first review the different kinds of variance.

Understanding different kinds of variance

There are four different kinds of variance as described in computer science literature. We will first describe them...

Parametric methods revisited

The ability to dispatch to various methods based upon subtyping relationships is a key feature of the Julia language. We initially introduced the concept of parametric methods in Chapter 3, Designing Functions and Interfaces. In this section, we will go a little deeper and examine some subtle situations about how methods are selected for dispatch.

Let's start with the basics: how do we specify type variables for parametric methods?

Specifying type variables

When we define a parametric method, we use the where clause to introduce type variables. Let's go over a simple example:

triple(x::Array{T,1}) where {T <: Real} = 3x

The triple function takes an Array{T}, where T is any subtype...

Summary

In this chapter, we learned about various topics related to subtyping, variance, and dispatch. These concepts are the fundamental building blocks for creating larger, more complex applications.

We first went over the topic of implementation inheritance and behavior subtyping and the differences between them. We reasoned that implementation inheritance is not a great design pattern because of various issues. We came to an understanding that Julia's type system is designed to avoid the flaws that we have seen in other programming languages.

Then, we reviewed different kinds of variance, which are nothing but ways to explain the subtyping relationship between parametric types. We walked through in great details how parametric types are invariant and method arguments are covariant. We then went even further to discuss the variance of function...

Questions

  1. How is implementation inheritance different from behavior subtyping?
  2. What are some major issues with implementation inheritance?
  3. What is duck typing?
  4. What is the variance of method arguments and why?
  5. Why are parametric types invariant in Julia?
  6. When does the diagonal rule apply?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Design Patterns and Best Practices with Julia
Published in: Jan 2020Publisher: PacktISBN-13: 9781838648817
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tom Kwong

Tom Kwong, CFA, is an experienced software engineer with over 25 years of industry programming experience. He has spent the majority of his career in the financial services industry. His expertise includes software architecture, design, and the development of trading/risk systems. Since 2017, he has uncovered the Julia language and has worked on several open source packages, including SASLib.jl. He currently works at Western Asset Management Company, a prestige asset management company that specializes in fixed income investment services. He holds an MS degree in computer science from the University of California, Santa Barbara (from 1993), and he holds the Chartered Financial Analyst designation since 2009.
Read more about Tom Kwong