Scala - 如何使 val 可见

发布于 2024-10-14 11:34:50 字数 261 浏览 3 评论 0原文

我有这个方法

def example(something):something {
  val c=List()
  if(){
    if(){
      val a=List()
    }
    else{
      val a=List()
    }
  }
  //here a or b are not declared
  c:::a
}

如何声明它并使其可见? 我不能使用var

I have this method

def example(something):something {
  val c=List()
  if(){
    if(){
      val a=List()
    }
    else{
      val a=List()
    }
  }
  //here a or b are not declared
  c:::a
}

How to declare it and make it visible? I can not use var.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懵少女 2024-10-21 11:34:50

您无法使其在声明范围之外可见,因此,也许可以尝试以下操作:

def example(somthing):somthing{    
  val c = { 

    if (something) {
      (0 to 10).toList
    } else {
      (0 to 5).toList
    }

  }
}

You can't make it visible outside declaration scope, so, maybe, try this:

def example(somthing):somthing{    
  val c = { 

    if (something) {
      (0 to 10).toList
    } else {
      (0 to 5).toList
    }

  }
}
还如梦归 2024-10-21 11:34:50

Scala 中几乎所有内容都会返回一个值(例外是诸如包声明和导入之类的语句)

if/else 语句、模式匹配等。

这意味着您的 if/else 块会返回一个值,并且特别热衷于这样做,非常像 Java 中的 ?: 三元运算符。

val foo = ... some integer ...
val bar = if(foo >= 0) "positive" else "negative"

或者使用块:

val foo = ... some integer ...
val bar = if(foo >= 0) {
  "positive"
} else {
  "negative"
}

如果您愿意,可以将它们嵌套到您的心目中!

val foo2 = ... some other integer ...
val bar = if(foo >= 0) {
  if(foo2 >= 0) {
    "double positive"
  } else {
    "part-way there"
  }
} else {
  "foo was negative"
}

甚至混合“n”匹配风格:

println(
  if(foo >= 0) {
    if(foo2 >= 0) "double positive" else "part-way there"
  } else {
    "foo was negative"
  }
)

Almost Everything in Scala returns a value (the exception is for statements such as package declarations and imports)

if/else statements, pattern matches, etc. etc.

This means that your if/else block returns a value, and is especially keen to do so, very much like the ?: ternary operator in Java.

val foo = ... some integer ...
val bar = if(foo >= 0) "positive" else "negative"

Or using blocks:

val foo = ... some integer ...
val bar = if(foo >= 0) {
  "positive"
} else {
  "negative"
}

Nest 'em to your heart's content if you wish!

val foo2 = ... some other integer ...
val bar = if(foo >= 0) {
  if(foo2 >= 0) {
    "double positive"
  } else {
    "part-way there"
  }
} else {
  "foo was negative"
}

and even mix 'n' match styles:

println(
  if(foo >= 0) {
    if(foo2 >= 0) "double positive" else "part-way there"
  } else {
    "foo was negative"
  }
)
谈场末日恋爱 2024-10-21 11:34:50

在您的特定情况下,您不需要 val a

def example(something):something {
  val c= yourListC ::: if(firstTest){
                         if(secondTest){
                           yourListA
                         }
                         else{
                           yourOtherListA
                         }
                       } else {
                           anAdequateEmptyList
                       }
}

In your particular case, you do not need the val a:

def example(something):something {
  val c= yourListC ::: if(firstTest){
                         if(secondTest){
                           yourListA
                         }
                         else{
                           yourOtherListA
                         }
                       } else {
                           anAdequateEmptyList
                       }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文