类 与类型参数的 Scala 隐式类型转换
Scala Implicit Type Conversion of Classes with Type Parameters
我正在尝试向 scala.collection.Iterable 特性添加功能,更具体地说,是一个遍历元素并将它们打印出来的打印函数(如果没有参数则打印到控制台,否则打印到输出流参数)。我正在使用我为对象 printSelf() 创建的预定义扩展方法。但是,这会导致编译器错误,'Value printSelf is not a member of type parameter Object.' 我还想将其作为一个单独的文件,以便我可以轻松地在多个项目和应用程序之间使用。
这是我当前的转换文件代码:
import java.io.OutputStream
import scala.collection.Iterable
package conversion{
class Convert {
implicit def object2SuperObject(o:Object) = new ConvertObject(o)
implicit def iterable2SuperIterable[Object](i:Iterable[Object]) = new ConvertIterable[Object](i)
}
class ConvertObject(o:Object){
def printSelf(){
println(o.toString())
}
def printSelf(os:OutputStream){
os.write(o.toString().getBytes())
}
}
class ConvertIterable[Object](i:Iterable[Object]){
def printerate(){
i.foreach {x => x.printSelf() }
}
def printerate(os:OutputStream){
i.foreach { x => x.printSelf(os) }
}
}
}
我在尝试对此进行测试的代码中也遇到了类似的错误,'value printerate is not a member of scala.collection.immutable.Range':
import conversion.Convert
package test {
object program extends App {
new testObj(10) test
}
class testObj(i: Integer) {
def test(){
val range = 0.until(i)
0.until(i).printerate()
}
}
}
我处理这种类型转换的方式有什么问题?
事实上有几件事:
- Convert 应该是一个对象,而不是 class。
- 您使用 Object 而不是 Any
- 您使用 Object 作为通用类型标识符,而不是更容易混淆的 T。
- 您没有导入隐式定义(仅导入对象本身是不够的)。
这应该有效:
package conversion {
object Convert {
implicit def object2SuperObject(o: Any) = new ConvertObject(o)
implicit def iterable2SuperIterable[T](i:Iterable[T]) = new ConvertIterable[T](i)
}
class ConvertObject(o: Any){
def printSelf(){
println(o.toString())
}
def printSelf(os:OutputStream){
os.write(o.toString().getBytes())
}
}
class ConvertIterable[T](i:Iterable[T]){
import Convert.object2SuperObject
def printerate(){
i.foreach {x => x.printSelf() }
}
def printerate(os:OutputStream){
i.foreach { x => x.printSelf(os) }
}
}
}
import conversion.Convert._
第二个文件:
package test {
object program extends App {
new testObj(10) test
}
class testObj(i: Integer) {
def test(){
val range = 0.until(i)
0.until(i).printerate()
}
}
}
我正在尝试向 scala.collection.Iterable 特性添加功能,更具体地说,是一个遍历元素并将它们打印出来的打印函数(如果没有参数则打印到控制台,否则打印到输出流参数)。我正在使用我为对象 printSelf() 创建的预定义扩展方法。但是,这会导致编译器错误,'Value printSelf is not a member of type parameter Object.' 我还想将其作为一个单独的文件,以便我可以轻松地在多个项目和应用程序之间使用。
这是我当前的转换文件代码:
import java.io.OutputStream
import scala.collection.Iterable
package conversion{
class Convert {
implicit def object2SuperObject(o:Object) = new ConvertObject(o)
implicit def iterable2SuperIterable[Object](i:Iterable[Object]) = new ConvertIterable[Object](i)
}
class ConvertObject(o:Object){
def printSelf(){
println(o.toString())
}
def printSelf(os:OutputStream){
os.write(o.toString().getBytes())
}
}
class ConvertIterable[Object](i:Iterable[Object]){
def printerate(){
i.foreach {x => x.printSelf() }
}
def printerate(os:OutputStream){
i.foreach { x => x.printSelf(os) }
}
}
}
我在尝试对此进行测试的代码中也遇到了类似的错误,'value printerate is not a member of scala.collection.immutable.Range':
import conversion.Convert
package test {
object program extends App {
new testObj(10) test
}
class testObj(i: Integer) {
def test(){
val range = 0.until(i)
0.until(i).printerate()
}
}
}
我处理这种类型转换的方式有什么问题?
事实上有几件事:
- Convert 应该是一个对象,而不是 class。
- 您使用 Object 而不是 Any
- 您使用 Object 作为通用类型标识符,而不是更容易混淆的 T。
- 您没有导入隐式定义(仅导入对象本身是不够的)。
这应该有效:
package conversion {
object Convert {
implicit def object2SuperObject(o: Any) = new ConvertObject(o)
implicit def iterable2SuperIterable[T](i:Iterable[T]) = new ConvertIterable[T](i)
}
class ConvertObject(o: Any){
def printSelf(){
println(o.toString())
}
def printSelf(os:OutputStream){
os.write(o.toString().getBytes())
}
}
class ConvertIterable[T](i:Iterable[T]){
import Convert.object2SuperObject
def printerate(){
i.foreach {x => x.printSelf() }
}
def printerate(os:OutputStream){
i.foreach { x => x.printSelf(os) }
}
}
}
import conversion.Convert._
第二个文件:
package test {
object program extends App {
new testObj(10) test
}
class testObj(i: Integer) {
def test(){
val range = 0.until(i)
0.until(i).printerate()
}
}
}