risc-v中文社区

 找回密码
 立即注册
查看: 1130|回复: 0

[原创] risc-v开发基础 Scala 操作符续---(13)

[复制链接]

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
发表于 2021-8-4 15:01:00 | 显示全部楼层 |阅读模式
scala2.10中采纳了SIP-18:模块化语言特性

引入的原因是scala中的某些特性比较难懂或有些晦涩,或有些特性是试验性质。
这些特性在普通场景下不鼓励使用,但在特定场景下又可能需要。所以引入了“显式引入语言特性”的做法。

举例来说,在2.10的repl下定义隐式转换:

scala> implicit def string2MyString(s:String) = new MyString(s)

warning: there were 1 feature warning(s); re-run with -feature for details
string2MyString: (s: String)MyString
好,我们重新启动repl,启用 -feature 选项:

$ scala -feature

scala> class MyString(str:String) { def say() {println(str)} }
defined class MyString

scala> implicit def string2MyString(s:String):MyString = new MyString(s)
<console>:8: warning: implicit conversion method string2MyString should be enabled
by making the implicit value language.implicitConversions visible.
This can be achieved by adding the import clause 'import scala.language.implicitConversions'
or by setting the compiler option -language:implicitConversions.
See the Scala docs for value scala.language.implicitConversions for a discussion
why the feature should be explicitly enabled.
   implicit def string2MyString(s:String):MyString = new MyString(s)
                ^
string2MyString: (s: String)MyString
这回给出了详细的警告信息,使用隐式转换方法,建议在代码中引入language单例的implicitConversions成员: import scala.language.implicitConversions 或者 编译时指定-language:implicitConversions参数

为什么要控制隐式转换这一特性(要显式启用)?是因为过度使用隐式转换会有很多陷阱,而现在已经有这个趋势,因为隐式转换的强大而被过度使用。另外,相对隐式转换,采用隐式参数对设计更好一些。

类似的控制还有后缀操作符:

scala> "hello" length //中间用空格,省略了点
也会给出警告提示import scala.language.postfixOps或编译时指定参数-language:postfixOps
这是因为后缀操作符与分号推理(semicolon inference)容易冲突,举个例子:

//有参方法:a.foo(b) 也可以写成 a foo b
//无参方法:a.foo() 也可以写成 a foo
//如果一个类中同时定义了上面两个方法
scala> class A { def foo(i:Int)=i+1; def foo()=2}

// 没有分号,解析为 a.foo(2)
scala> val r = { a foo 2 }
r: Int = 3

// 有分号的话解析为 a.foo() 分号后边的是下一条语句
scala> val r = { a foo; 2 }
warning: there were 1 feature warning(s); re-run with -feature for details
r: Int = 2

// 但在换行情况下
scala> :pas
// Entering paste mode (ctrl-D to finish)
val r = {
    a foo // 这里没写分号
    2
}
// Exiting paste mode, now interpreting.

r: Int = 3
结果被解析为 a.foo(3) !!
如果没有分号,就无法解析为a.foo(),总会把下一条语句也当作参数,比如:

scala> :pas
// Entering paste mode (ctrl-D to finish)
var r = {
    a foo // 这里没有写分号
    println("another")
}
// Exiting paste mode, now interpreting.

<console>:19: error: type mismatch;
found   : Unit
required: Int
    println("another")
           ^
所以为了避免这种情况,后缀操作符也不鼓励,只有特定场景需要时(DSL),显式的打开这个选项;否则会警告。

另外不鼓励的特性还有:存在类型(existentials)、高阶类(higherKinds)、动态类(dynamics)、反射调用(reflectiveCalls)

本帖内容来自:此网页链接

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-5-3 19:23 , Processed in 0.014947 second(s), 17 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表