learning scala How To Create Implicit Function
2024-08-27 10:53:15
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
最新文章
- linux搭载discuz
- DFD数据流程图
- 剑指offer系列40----机器人的运动范围
- java常见面试题
- hdu 3966 Aragorn&;#39;s Story(树链剖分+树状数组)
- GBDT详解
- 使用imageLoader加载图片资源
- how tomcat works 六 生命周期
- MongoDB常用配置项目
- 展开被 SpringBoot 玩的日子 《 三 》 整合Redis
- Java基础12:深入理解Class类和Object类
- 测者的测试技术手册:自动的自动化框架EvoSuite集成Cobertura得到可视化的代码覆盖报告
- java Reference
- 13 Tensorflow API主要功能
- forfiles命令详解
- 社交网络编程API之iOS系统自带分享
- Lab 1-1
- 转 linux 权限
- 四、curator recipes之共享重入互斥锁
- [ldap]ldap server安装以及图形化操作
热门文章
- PAT甲级题分类汇编——线性
- C++ new/delete详解及原理
- ALV报表——基础(一)
- VS.NET(C#)--2.3良构的XHTML
- centos7.x安装docker-ce
- 【日语】日语能力考试N2级核心词汇必备—接续词
- py-2 python介绍与安装
- 【robotframework】robotframework基本使用
- xshell退出保持后台服务运行的方法
- Java基本知识点o(1), o(n), o(logn), o(nlogn)的了解