zoukankan      html  css  js  c++  java
  • 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("
    Step 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("
    Step 3: How to import the String conversion so that it is in scope")
    import DonutConverstions._
    
    println("
    Step 4: How to create String values")
    val glazedDonut = "Glazed Donut"
    val vanillaDonut = "Vanilla Donut"
    
    println("
    Step 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
    

      

  • 相关阅读:
    关于本Blog无法进行评论问题的说明
    Apusic Operamasks例子部署过程
    JVM启动参数(转)
    20070724中间件产品培训方式总结
    写Blog的意义
    磁碟機讀取光碟片時遇故障7/9
    .aspx沒有語言擴充功能8/14
    水晶報表公式的dateadd及cstr應用7/11
    中毒了:Trojar6/26
    學位英語考試通過啦8/28
  • 原文地址:https://www.cnblogs.com/lianghong881018/p/11171533.html
Copyright © 2011-2022 走看看