简体   繁体   English

如果语句中的 Scala 类型不匹配

[英]Scala type mismatch in if statement

Wondering why this code is not working, Am I missing something?想知道为什么这段代码不起作用,我错过了什么吗?

def treemin(lst: List[Any]): Int = {
  var MaVar: Int = 1000{
  case (sum,leaf: Int)  =>
    if(leaf < MaVar) {MaVar = leaf}
  }
}

The error I have occur here:我在这里发生的错误:

if(leaf < MaVar) {MaVar = leaf}

Error:错误:

Error: type mismatch;错误:类型不匹配; found: Unit required: Int找到:所需单位:Int

I already had a look here but I didn't manage to solve this issue since I am new to scala, it might be a silly error.我已经看过这里,但我没有设法解决这个问题,因为我是 scala 的新手,这可能是一个愚蠢的错误。

Note: Is this a good approach to get the minimum leaf of a tree?注意:这是获得树的最小叶子的好方法吗?

I got a Tree:我得到了一棵树:

在此处输入图像描述

And I am trying to do a function which will return the min leaf, for example here it would return 2.我正在尝试做一个 function 它将返回最小叶,例如这里它将返回 2。

There are actually two errors in your code:您的代码中实际上有两个错误:

def treemin(lst: List[Any]): Int = {
  var MaVar: Int = 1000{
  case (sum,leaf: Int)  =>
    if(leaf < MaVar) {MaVar = leaf}
  }
}

The first error is this one:第一个错误是这个:

         var MaVar: Int = 1000{
                              ^
On line 2: error: Int(1000) does not take parameters
       }
       ^

Here, you are trying to call 1000 with an argument, as if it were a function.在这里,您尝试使用参数调用1000 ,就好像它是 function。 It is not quite clear what you are trying to achieve here.目前尚不清楚您要在这里实现什么。 Are you trying to create a new lexical scope?您是否正在尝试创建新的词汇 scope? Then, you need to add an empty line so that Scala doesn't interpret your curly braces as an argument to 1000 :然后,您需要添加一个空行,以便 Scala 不会将您的花括号解释为1000的参数:

def treemin(lst: List[Any]): Int = {
  var MaVar: Int = 1000

  {
    case (sum,leaf: Int)  =>
      if(leaf < MaVar) {MaVar = leaf}
  }
}

However, now you get another error, because now the curly braces are interpreted as creating an anonymous function:然而,现在你得到另一个错误,因为现在花括号被解释为创建一个匿名 function:

         {
         ^
On line 4: error: missing parameter type for expanded function
       The argument types of an anonymous function must be fully known. (SLS 8.5)
       Expected type was: Int

There are ways to work around that, but the thing is: it doesn't make sense to create a new lexical scope here, since you don't define any variables in it, so there is nothing to "scope" in the first place.有一些方法可以解决这个问题,但问题是:在这里创建一个新的词法 scope 是没有意义的,因为你没有在其中定义任何变量,所以首先没有什么可以“作用域” .

Just get rid of it:摆脱它:

def treemin(lst: List[Any]): Int = {
  var MaVar: Int = 1000
  case (sum,leaf: Int)  =>
    if(leaf < MaVar) {MaVar = leaf}
}

Now, we get another error:现在,我们得到另一个错误:

         case (sum,leaf: Int)  =>
         ^
On line 3: error: '}' expected but 'case' found.

Which essentially tells us that a case should go together with a match statement.这基本上告诉我们一个case应该是 go 和一个match语句。 Since it is not really clear to me what you are trying to match against, I cannot fix the code any further.由于我不清楚您要匹配的内容,因此我无法进一步修复代码。

This brings us to the second error, which is actually pretty simple:这给我们带来了第二个错误,这实际上非常简单:

       }
       ^
On line 6: error: type mismatch;
        found   : Unit
        required: Int

This is pointing to the closing curly brace of your method, and it says essentially that you promised in your method definition that the method returns an Int , but you are actually returning Unit .这是指向您的方法的右花括号,它本质上说您在方法定义中承诺该方法返回一个Int ,但实际上您返回的是Unit In fact, you are not explicitly returning anything at all .事实上,你根本没有明确地返回任何东西 If you don't explicitly return anything, then the value of the last expression evaluated inside the method is returned.如果您没有显式返回任何内容,则返回方法内计算的最后一个表达式的值。

In this case, it is actually hard to understand what the last expression that is evaluated is going to be, since there is an error in the method, and so the method will never be evaluated at all anyway.在这种情况下,实际上很难理解评估的最后一个表达式将是什么,因为方法中存在错误,因此无论如何该方法永远不会被评估。 But, let's look at the two candidates:但是,让我们看看这两个候选人:

var MaVar: Int = 1000

This is an assignment.这是一个任务。 Assignments evaluate to Unit , so if this were the last expression evaluated, then the return value would indeed be () which is the singleton instance of Unit .赋值计算为Unit ,所以如果是最后一个计算的表达式,那么返回值确实是() ,它是Unit的 singleton 实例。

The other candidate for the last expression evaluated inside the method is the conditional expression:在方法内部计算的最后一个表达式的另一个候选是条件表达式:

if(leaf < MaVar) {MaVar = leaf}

Now, what is the value of a conditional expression?现在,条件表达式的值是多少? Well, it is the value of whatever branch was taken, and the type of the conditional expression is the lowest common ancestor of the types of the two branches.嗯,它是任何分支的值,条件表达式的类型是两个分支类型的最低共同祖先。

The "true" branch in your conditional expression is again an assignment, and as we established above, an assignment evaluates to () aka Unit .条件表达式中的“真”分支又是一个赋值,正如我们上面建立的,赋值计算为() aka Unit

The "false" branch in your conditional doesn't exist , but it has to evaluate to something if it is taken, because in Scala, everything is an expression (there are no statements) and thus everything evaluates to a value .条件中的“假”分支不存在,但如果被采用,它必须评估为某些东西,因为在 Scala 中,一切都是表达式(没有语句),因此一切都评估为 value Well, the value that represents the absence of a sensible return value is (you guessed it) () aka Unit .好吧,表示没有合理返回值的值是(你猜对了) () aka Unit

So, however you look at it, your method always returns Unit whereas it actually promises to return an Int .所以,无论你怎么看,你的方法总是返回Unit而它实际上承诺返回一个Int That's why get the type error.这就是为什么会出现类型错误。

There are a couple of other oddities with your method: It takes an argument, but never uses it.您的方法还有其他一些奇怪之处:它需要一个参数,但从不使用它。 And, the argument is of type List[Any] , which is a huge red flag.而且,参数是List[Any]类型,这是一个巨大的危险信号。 Scala has a very powerful type system, there is almost never the case that you don't know what specific type something is. Scala 有一个非常强大的类型系统,几乎从来没有你不知道某个东西具体是什么类型的情况。 Any is the most useless type, since it does not tell you anything about what the type is and what it can do. Any是最没用的类型,因为它没有告诉你任何关于类型是什么以及它可以做什么的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM