Scala中的弃用警告

3

我定义了一个名为 ti 的函数,但编译后出现了弃用警告。我在Eclipse中也做了同样的操作。代码可以正常运行,但会显示警告。

scala>  def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)] = {
     |    if(chars.length!=0) {
     |      var j = 1
     |      var c = chars.head
     |      for(i<-chars.tail) {
     |        if(c==i) j=j+1
     |      }
     |      a::List((c,j))
     |      ti(chars.tail,a)
     |   }
     |   else a
     | }

警告:有3个废弃警告;重新运行时使用-deprecation参数查看详细信息 ti: (chars: List[Char], a: List[(Char, Integer)])List[(Char, Integer)]

这是什么原因呢?

1个回答

8
如果它告诉你要使用 -deprecation 重新运行,那么你应该这样做:
k@k:~$ scala -deprecation
Welcome to Scala version 2.9.1 (OpenJDK 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)]=
     |         { if(chars.length!=0)
     |            {
     |            var j=1
     |            var c=chars.head
     |            for(i<-chars.tail)
     |            {
     |            if(c==i) j=j+1
     |            }
     |            a::List((c,j))
     |            ti(chars.tail,a)
     |            }
     |         else a
     |         }

然后你会收到警告:
<console>:7: warning: type Integer is deprecated: use java.lang.Integer instead
       def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)]=
                                 ^
<console>:7: warning: type Integer is deprecated: use java.lang.Integer instead
       def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)]=
                                                       ^
<console>:16: warning: type Integer is deprecated: use java.lang.Integer instead
                  a::List((c,j))
                   ^

这里问题是,如果您不需要与Java进行互操作性,应该使用java.lang.Integer或只是Int。因此,您可以import java.lang.Integer,或将Integer更改为Int


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接