其他分享
首页 > 其他分享> > learning scala How To Create Implicit Function

learning scala How To Create Implicit Function

作者:互联网

println("Step 1: How to create a wrapper String class which will extend the String type")
class DonutString(s: String) {

  def isFavoriteDonut: Boolean = s == "Glazed Donut"

}

println("\nStep 2: How to create an implicit function to convert a String to the wrapper String class")
object DonutConverstions {
  implicit def stringToDonutString(s: String) = new DonutString(s)
}


println("\nStep 3: How to import the String conversion so that it is in scope")
import DonutConverstions._

println("\nStep 4: How to create String values")
val glazedDonut = "Glazed Donut"
val vanillaDonut = "Vanilla Donut"

println("\nStep 5: How to access the custom String function called isFavaoriteDonut")
println(s"Is Glazed Donut my favorite Donut = ${glazedDonut.isFavoriteDonut}")
println(s"Is Vanilla Donut my favorite Donut = ${vanillaDonut.isFavoriteDonut}")

result

Step 1: How to create a wrapper String class which will extend the String type

Step 2: How to create an implicit function to convert a String to the wrapper String class

Step 3: How to import the String conversion so that it is in scope

Step 4: How to create String values

Step 5: How to access the custom String function called isFavaoriteDonut
Is Glazed Donut my favorite Donut = true
Is Vanilla Donut my favorite Donut = false

  

标签:Function,String,Donut,Create,scala,How,Step,println,create
来源: https://www.cnblogs.com/lianghong881018/p/11171533.html