English 中文(简体)
对不变价值使用折价的影响是什么
原标题:What are the implications of using def vs. val for constant values

What are the implications of using def vs. val in Scala to define a constant, immutable value? I obviously can write the following:

val x = 3;
def y = 4;
var a = x + y; // 7

这两项声明有什么区别? 谁做得更好? 建议采取什么方式? 何时,我会把一人用在另一边?

最佳回答

Assuming these are class-level declarations:

汇编者将制作<代码>valfinal,这可导致由考试和测验科采用更为优化的代码。

www.un.org/Depts/DGACM/index_spanish.htm 赢得了标本的储存价值,因此可以节省记忆,但需要每次评估的方法。

就两个世界而言,都成为附庸标,并宣布定型号为val

i.e. 而不是

class Foo {
  val MyConstant = 42
}

因此:

class Foo {}

object Foo {
  val MyConstant = 42
}
问题回答

<val一经评估并储存在外地。 <代码>def作为方法实施,每次重新评价,但并不使用记忆空间储存所产生的价值。





相关问题
Why I can t pass two chars as function arguments in C?

I have a function: int get_symbol(tab *tp, FILE *fp, char delim) and I call it like this: get_symbol(tp, fp, ; ) I always have it declared in the header as: int get_symbol(tab *, FILE *, char); ...

problem w/ linking static function g++

I am trying to build a small program and I have my own library libfoo. I have a camera class that is calling a static function from my Vector3 class (i.e. crossProduct). My camera class and Vector3 ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

Def, Void, Function?

Recently, I ve been learning different programming langages, and come across many different names to initalize a function construct. For instance, ruby and python use the def keyword, and php and ...

热门标签