KoreanFoodie's Study

Scala 9 - Sub Types 본문

Tutorials/Scala

Scala 9 - Sub Types

GoldGiver 2019. 4. 23. 19:06


스칼라 튜토리얼, scala의 sub types에 대해 알아보자


Sub Type Polymorphism (Concept)

Let's assume that we want this:

object tom {
    val name = "Tom"
    val home = "02-880-1234"
}

object bob {
    val name = "Bob"
    val home = "02-123-1234"
}

def greeting(r: ???) = "Hi " + r.name + ", How are you?"

greeting(tom)
greeting(bob)

// Note that we have

tom: {val name: String; val home: String}
bob: {val name: String; val mobile: String}

What about we use sub types?

type NameHome = {val name: String; val home: String}
type NameMobile = {val name: String; val mobile: String}
type Name = {val name: String}

NameHome <: Name     // (NameHome is a sub type of Name)
NameMobile <: Name  // (NameMobile is a sub type of Name)

def greeting(r: Name) = "Hi " + r.name + ", How are you?"

greeting(tom)
greeting(bob)

The sub type relation is kind of the subset relation, but they are NOT the same.

  • T <: S

Every element of T can be used as that of S.

  • Cf. T is a subset of S.

Every element of T is that of S.

  • Why polymorphism?

A function of type S=>R can be used as T=>R for many sub types T of S.

Note that S=>R <:T=>R when T<:S.


Two Kinds of Sub Types

  • Structural Sub Types

The system implicitly determines the sub type relation by the
structures of data types.

Structurally equivalent types are the same.

  • Nominal Sub Types

The user explicitly specify the sub type relation using the names of
data types.

Structurally equivalent types with different names may be different.


Structural Sub Types

  • General Sub type

Reflexivity:

For any type T, we have:
T <: T

Transivity:

For any types T,S,R, we have:
T <: R R <: S
====================
T <: S


Sub Types for Special Types

  • Nothing : The empty set

  • Any : The set of all values

  • For any type T, we have:

    Nothing <: T <: Any

  • Example:

val a: Int = 3
val b: Any = a
def f(a: Nothing): Int = a
// def g(a: Any): Int = a     => There is no elimination form!

Nothing makes impossible possible. It can make anything!

Any can be any type, but does not give any useful information! So, It is just garbage! You can't do anything! (Pardon the pun)


Sub Types for Records

  • Permutation

          =======================

    {…; x: T1; y: T2; …} <: {…; y: T2, x: T1; … }

  • Width

          =======================
          {…; x: T; …} <: { …; … }
  • Depth

                  T <: S
          =======================
      {…;  x: T ; …} <: { …; x: S; … }
  • Example

    {val x: { val y: Int; val z: String}, val w: Int}
    <:             // (by permutation)
    {val w: Int; val x: { val y: Int; val z: String}}
    <:             // (by depth & width)
    {val w: Int; val x: {val z: String}}

Sub type trasnformation acts as if 'type casting', but doesn't involve any computation!


Sub Types for Functions

  • Function Sub Type
            T <: T’      S <: S’ 
          =======================
             (T’=>S) <: (T=>S’)
  • Tuple sub type
            T1 <: S1        T2 <: S2
            ========================
              (T1, T2) <: (S1, S2)
  • Example
def foo(s: {val a: Int; val b: Int}):
    {val x: Int; val y: Int} = {
        object tmp {
            val x = s.b
            val y = s.a
        }
        tmp
    }

val gee:
    {val a: Int; val b: Int; val c: Int} =>
    {val x: Int} =
    foo _
  • Exercise 1

If (T1=>T2)=>T3 <: (T1'=>T2')=>T3' holds, clarify all the relation between them.

  • Solution 1

Use sub type rules.

Since (T1'=>T2') <: (T1=>T2), we can interpret them into...
T1 <: T1', T2' <: T2
Similary, we can conclude that T3 <: T3'
  • Exercise 2

If T1=>(T2=>T3) <: T1'=>(T2'=>T3') holds, clarify all the relation between them.

  • Solution 2
Since (T2=>T3) <: (T2'=>T3'), we can interpret them into...
T2' <: T2, T3 <: T3'
Similary, we can conclude that T1' <: T1

'Tutorials > Scala' 카테고리의 다른 글

Scala 11 - Abstract Class, Basics  (0) 2019.04.24
Scala 10 - Class  (0) 2019.04.23
Scala 8 - Parametric Polymorphism  (0) 2019.04.23
Scala 7 - Datatypes2  (0) 2019.04.23
Scala 6 - Datatypes  (0) 2019.04.23
Comments