在Scala中评估val,var,lazy val和def构造时

发表简短目录 (Post Brief TOC)

  • Introduction

    介绍
  • Scala ‘val’ usage

    Scala“ val”用法
  • How Scala ‘val’ is Evaluated?

    如何评估Scala的“ val”?
  • Scala ‘var’ usage

    Scala“ var”用法
  • How Scala ‘var’ is Evaluated?

    如何评估Scala'var'?
  • Scala ‘def’ usage

    Scala'def'用法
  • How Scala ‘def’ is Evaluated?

    Scala的“ def”如何评估?
  • How Scala ‘lazy val’ is Evaluated?

    如何评估Scala的“懒惰值”?
  • var Vs lazy val

    var Vs lazy val
  • Scala val,var,lazy val and def Constructs In-Brief

    Scala val,var,lazy val和def构造In-Brief

介绍 (Introduction)

In this post, we are going to discuss about some basic concepts of Scala Programming Language. Even though, it covers very basic concepts but they are very important to know.

在本文中,我们将讨论Scala编程语言的一些基本概念。 即使它涵盖了非常基本的概念,但了解它们也非常重要。

We can answer some questions like What is val, var and def? What are the differences between them? When they are evaluated? What are the differences between val and lazy val?. These things are required to start working on Scala-Based Projects

我们可以回答一些问题,例如什么是val,var和def? 它们之间有什么区别? 什么时候进行评估? val和lazy val有什么区别? 开始基于Scala的项目时需要这些东西

Scala“ val”用法 (Scala ‘val’ usage)

In Scala, ‘val’ modifier is used to define constants. val means constant or immutable that means we cannot change it’s value once its created.

在Scala中,“ val”修饰符用于定义常量。 val表示常量或不可变,表示一旦创建它便无法更改其值。

Example:-

例:-

scala> val name:String = "Scala"
name: String = Scala

scala> name
res2: String = Scala

scala> name = "Java"
:11: error: reassignment to val
       name = "Java"
            ^

Here, we can observe that reassignment to val is prohibited. That means val is used to define Immutable data.

在这里,我们可以看到禁止重新分配给val。 这意味着val用于定义不可变数据。

如何评估Scala的“ val”? (How Scala ‘val’ is Evaluated?)

In Scala, ‘val’ is used to define constants. It evaluates only once. They are evaluated at the time of definition only. Once its evaluated, it reuses same value for all references of it.

在Scala中,“ val”用于定义常量。 它只评估一次。 仅在定义时评估它们。 一旦对其求值,它将为所有引用重复使用相同的值。

It does NOT evaluate every-time we access it.

它不会在我们每次访问它时进行评估。

We will explore this rule by using one simple example as shown below:

我们将通过一个简单的示例来探索此规则,如下所示:

Example:-

例:-

object ValApp extends App{
  val number = { println("Constant number is initialized."); 99 }
  println("Before Accessing 'number' constant:")
  println(number + 1)
  println(number + 1)
  println(number + 1)
}

Output:-
Here I’m using all REPL commands for simplicity purpose only. We can test them using any Scala IDEs like Eclipse Scala IDE or IntelliJ IDE (My favourite IDE).

输出:-
在这里,我仅出于简化目的使用所有REPL命令。 我们可以使用任何Scala IDE(例如Eclipse Scala IDE或IntelliJ IDE(我最喜欢的IDE))对其进行测试。

F:\>scalac ValApp.scala

F:\>scala ValApp
Constant number is initialized.
Before Accessing 'number' constant:
100
100
100

Here we can observe that “number” Constant is evaluated only once at the time of definition that’s why println output is printed before accessing the variable. Even though we have accessed number Constant three times, it’s initialized only once and printed “Constant number is initialized.” text only once.

在这里,我们可以观察到“数字”常量在定义时仅被评估一次,这就是为什么在访问变量之前先打印println输出的原因。 即使我们已经访问过3次常数,它也只能初始化一次并显示“常数已初始化”。 仅发送一次文字。

Scala“ var”用法 (Scala ‘var’ usage)

In Scala, ‘var’ modifier is used to define variables. var means Variable or Mutable that means we can change it’s value once its created.

在Scala中,“ var”修饰符用于定义变量。 var表示Variable或Mutable,表示一旦创建它就可以更改其值。

Example:-

例:-

scala> var name:String = "Scala"
name: String = Scala

scala> name
res0: String = Scala

scala> name = "Java"
name: String = Java

scala> name
res1: String = Java

Here, we can observe that reassignment to var is allowed. As var is used to define Mutable data, we can change its value once its created.

在这里,我们可以看到允许重新分配给var。 由于var用于定义Mutable数据,因此我们可以在创建变量后更改其值。

如何评估Scala'var'? (How Scala ‘var’ is Evaluated?)

var is evaluated at the time of definition
It is evaluated only once

在定义时评估var
仅评估一次

Example:-

例:-

object VarApp extends App{
  val number = { println("Variable number is initialized."); 99 }
  println("Before Accessing 'number' variable:")
  println(number + 1)
  println(number + 1)
  println(number + 1)
}

Output:-

输出:-

F:\>scalac VarApp.scala

F:\>scala VarApp
Variable number is initialized.
Before Accessing 'number' variable:
100
100
100

Scala'def'用法 (Scala ‘def’ usage)

In Scala, def is used to define functions or methods. A Method or Function may or may not have arguments and may or may not have return type.

在Scala中,def用于定义函数或方法。 方法或函数可能具有或不具有参数,并且可能具有或不具有返回类型。

Example:-

例:-

scala> def add(num1:Int, num2:Int): Int = num1 + num2
add: (num1: Int, num2: Int)Int

scala> add(11,22)
res2: Int = 33

As shown above, Method or Function is evaluated only we make a call to it.

如上所示,仅在调用方法或函数时才对其进行评估。

Scala的“ def”如何评估? (How Scala ‘def’ is Evaluated?)

In Scala, def is evaluated lazily. It is not evaluated at the time of definition. It is evaluated whenever we make a call to it. It is evaluated every-time we make a call to it.

在Scala中,def的计算是延迟的。 在定义时不进行评估。 每当我们调用它时,都会对其进行评估。 每次调用它时都会对其进行评估。

Example:-

例:-

object DefApp extends App{
  def tax = {
    println("Function execution started.")
    1100
  }

  println(tax)
  println(tax)
  println(tax)
}

Output:-

输出:-

Before making a cll to Function:
Function execution started.
1100
Function execution started.
1100
Function execution started.
1100

As shown above output, method or function is evaluated only when we make a call to it. Otherwise, it is not evaluated.

如上面的输出所示,仅在调用方法或函数时才对其进行评估。 否则,将不进行评估。

如何评估Scala的“懒惰值”? (How Scala ‘lazy val’ is Evaluated?)

As we know, val is used to define constants or Immutable Data. In the same way, we use “lazy val” to define Immutable data.

众所周知,val用于定义常量或不可变数据。 同样,我们使用“惰性值”定义不可变数据。

However, lazy val is evaluated lazily and only once. It is evaluated only once when we use it for first time.
It is not evaluated at the time of definition. It is not evaluated every-time we access it.

但是,懒惰val只能被懒惰地评估一次。 当我们第一次使用它时,它只会被评估一次。
在定义时不进行评估。 我们不会在每次访问它时对其进行评估。

Example:-

例:-

object LazyValApp extends App{
  lazy val number = { println("Constant number is initialized."); 99 }
  println("Before Accessing 'number' constant:")
  println(number + 1)
  println(number + 1)
  println(number + 1) 
}

Output:-

输出:-

Before Accessing 'number' constant:
Constant number is initialized.
100
100
100

Here I’m using same example of ValApp, but changed from “val” to “lazy val” that’s it.

在这里,我使用的是ValApp的相同示例,但仅从“ val”更改为“ lazy val”。

If we observe this output, we can see that we are not seeing “Constant number is initialized.” output at the time of definition. We are seeing this message only at the time of first access. It’s displaying that message only once right. Cool!.

如果观察到此输出,则可以看到没有看到“常量号已初始化”。 定义时的输出。 我们仅在首次访问时才看到此消息。 它仅一次显示该消息。 凉!。

NOTE:- We cannot use “lazy” modifier for var. It’s allowed to use only for val.
Example:-

注意:-我们不能对var使用“惰性”修饰符。 只允许用于val。
例:-

scala> lazy var a = 0
:1: error: lazy not allowed here. Only vals can be lazy
lazy var a = 0
     ^

var Vs lazy val (var Vs lazy val)

Here we will discuss some similarities and differences between val and lazy val constructs in Scala Language.

在这里,我们将讨论Scala语言中val和惰性val构造之间的一些异同。

Similarities between val and lazy val constructs:-

val和惰性val构造之间的相似之处:

  • Both are used to define constants or Immutable Data

    两者都用于定义常量或不可变数据
  • Both are evaluated only once.

    两者仅被评估一次。

Differences between val and lazy val constructs:-

val和惰性val构造之间的区别:

  • “val” is evaluated at the time of definition that means Eagerly.

    “ val”是在定义时评估的,表示“渴望”。
  • “lazy val” is evaluated only when we access it that means Lazily.

    仅当我们访问“惰性值”时才表示“惰性值”。

Scala val,var,lazy val和def构造In-Brief (Scala val,var,lazy val and def Constructs In-Brief)

In previous sections, we have discussed about Scala val,var,lazy val and def Constructs in-detail with some simple and useful examples. Here are some bullet points to remember.

在前面的部分中,我们通过一些简单而有用的示例详细讨论了Scala val,var,lazy val和def构造。 这里有一些要记住的要点。

  • “val” is used to define Immutable data. It’s evaluated only once at the time of definition.

    “ val”用于定义不可变数据。 在定义时仅评估一次。
  • “var” is used to define Mutable data. It’s evaluated only once at the time of definition.

    “ var”用于定义可变数据。 在定义时仅评估一次。
  • Both val and var are evaluated Eagerly.

    认真评估val和var。
  • “lazy val” is used to define Immutable data. It is evaluated only once when we access it for first time. That means it is evaluated Lazily.

    “惰性值”用于定义不可变数据。 当我们第一次访问它时,它只会被评估一次。 这意味着它被懒惰地评估。
  • “def” is used to define Methods or Functions. It is evaluated only when we access it and evaluated every-time we access it. That means it is evaluated Lazily.

    “ def”用于定义方法或函数。 仅当我们访问它时才对其进行评估,并在每次访问时对其进行评估。 这意味着它被懒惰地评估。

NOTE:-In Scala, “by-name” arguments are evaluated/computed every time we access them. As it’s very big concept, we will discuss about it in a separate post soon.

注意:-在Scala中,每次访问它们时,都会对“按名称”参数进行评估/计算。 由于它是一个非常大的概念,因此我们很快将在另一篇文章中进行讨论。

That’s it all about Scala val,var, def and lazy val constructs usage and evaluation. We will discuss some more Scala concepts in my coming posts.

这就是关于Scala val,var,def和lazy val构造用法和评估的全部内容。 我们将在我的后续文章中讨论更多Scala概念。

Please drop me a comment if you like my post or have any typo errors/issues/suggestions.

如果您喜欢我的帖子或有任何错字/问题/建议,请给我评论。

翻译自: https://www.journaldev.com/11930/scala-val-var-lazyval-def-constructs

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值