- Learn Scala Programming
- Slava Schmidt
- 298字
- 2021-06-10 19:35:49
Polymorphic functions
We have already used polymorphic functions when we played with Glass examples in the previous chapter:
sealed trait Glass[+Contents]
case class Full[Contents](c: Contents) extends Glass[Contents]
case object EmptyGlass extends Glass[Nothing]
case class Water(purity: Int)
def drink(glass: Glass[Water]): Unit = ???
scala> :type drink _
Glass[Water] => Unit
The drink method is monomorphic and thus can only be applied to the argument of the type Glass[Water], not even for an EmptyGlass. Of course, we don't want to implement a separate method for every possible type of content out there. Instead, we implement our functions in a polymorphic way:
def drinkAndRefill[C](glass: Glass[C]): Glass[C] = glass
drinkAndRefill: [C](glass: Glass[C])Glass[C]
scala> :type drinkAndRefill _
Glass[Nothing] => Glass[Nothing]
scala> :type drinkAndRefill[Water] _
Glass[Water] => Glass[Water]
The type parameter is available in the body of the method. In this case, we specify that the result should have the same type of content as the argument.
Of course, it is possible to further constrain type parameter as we did before:
def drinkAndRefillWater[B >: Water, C >: B](glass: Glass[B]): Glass[C] = glass
scala> :type drinkAndRefillWater[Water, Water] _
Glass[Water] => Glass[Water]
Here, our method accepts any glass as long as it is a glass of water and allows anything more specific than water to be filled in it.
Both examples also demonstrate that we can specify a type parameter during the partial application in order to have a monomorphic function of some specific type. Otherwise, the compiler applies the bottom type parameter the same way as it does when we define a polymorphic function using a function literal:
scala> def drinkFun[B] = (glass: Glass[B]) => glass
drinkFun: [B]=> Glass[B] => Glass[B]
scala> :type drinkFun
Glass[Nothing] => Glass[Nothing]
scala> drinkFun(Full(Water))
res17: Glass[Water.type] = Full(Water)
The inferred result type is correct at the moment of function application.
- 深度實踐OpenStack:基于Python的OpenStack組件開發
- VMware View Security Essentials
- R語言數據分析從入門到精通
- 實戰Java程序設計
- PLC編程與調試技術(松下系列)
- 從0到1:Python數據分析
- Nginx實戰:基于Lua語言的配置、開發與架構詳解
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- GameMaker Essentials
- Learning AWS
- Orchestrating Docker
- Android 游戲開發大全(第二版)
- 啊哈C語言?。哼壿嫷奶魬穑ㄐ抻啺妫?/a>
- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)
- C語言程序設計實驗指導教程