Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Scala中引入新变量的方法有哪些?_Scala_Variable Declaration - Fatal编程技术网

在Scala中引入新变量的方法有哪些?

在Scala中引入新变量的方法有哪些?,scala,variable-declaration,Scala,Variable Declaration,众所周知,程序员可以使用val或var在Scala中声明新变量,如下所示: val x = 10 //x is now defined and an Integer. 函数参数还引入了一个新变量: def fun(y: String) { //y of type String is now available here } 这些都是直截了当的例子。但是,在给定的上下文中有更多的方法来声明变量 例如,match表达式也可以引入新变量: val z = 10 z match {

众所周知,程序员可以使用
val
var
在Scala中声明新变量,如下所示:

val x = 10 //x is now defined and an Integer.
函数参数还引入了一个新变量:

def fun(y: String) {
    //y of type String is now available here
}
这些都是直截了当的例子。但是,在给定的上下文中有更多的方法来声明变量

例如,
match
表达式也可以引入新变量:

val z = 10
z match {
     case w: Int => w //w is a valid variable of type Int in this scope
}
在Scala中将变量引入特定范围的其他命令有哪些

感兴趣人士的背景:

我在一个宏中使用它,该宏在抽象语法树中查找变量定义(valdef)<代码>匹配表达式或函数定义生成与普通valdef不同的语法树,我必须处理这些语法树。因为我希望我的宏是健壮的,所以我想针对所有可能的变量声明形式对它进行测试

评论说明:

带有
def
的方法定义不受关注。此外,我只对源代码中可见的变量感兴趣,这些变量可以被某些术语引用

解构绑定:

case class CaseClassFiftyThree(x: Double, y: Long, z: String)
...
someValue match { case CaseClassFiftyThree(x, y, z) =>
  /* x, y and z are bound here as Double, Long and String, resp. */ }
无可辩驳的模式匹配:

val (r, s, t) = (53, 17.0 * 3 + 2, "LIII")

/* r, s and t are bound here as Int, Double and String, resp. */
解构绑定:

case class CaseClassFiftyThree(x: Double, y: Long, z: String)
...
someValue match { case CaseClassFiftyThree(x, y, z) =>
  /* x, y and z are bound here as Double, Long and String, resp. */ }
无可辩驳的模式匹配:

val (r, s, t) = (53, 17.0 * 3 + 2, "LIII")

/* r, s and t are bound here as Int, Double and String, resp. */

这是我所知道的可能不同的一切的清单
x
是创建的变量:

// Within a block
val x = 5
var x = 5
lazy val x = 5
def x = 5
object x { val value = 5 }
val MyCaseClass(x, _) = oneOfMyCaseClasses
val MyCaseClass(_, Another(x)) = foo
val MyCaseClass(_, x @ Another(_)) = foo

// Functions
m.map( x => bar(x) )
m.map( (x: Int) => bar(x) )

// Functions that destructure
m.map{ case y if foo(y) => baz; case x => bar(x) }
m.map{ case Option(x) => bar(x) }
m.map{ case Option(List(x)) => bar(x) }
m.map{ case Option(x @ List(_)) => foo(x) }

// Partial functions with/without destructuring
m.collect{ case x => bar(x) }
m.collect{ case Option(List(x)) => bar(x) }
m.collect{ case Option(x @ List(_)) => foo(x) }

// For comprehensions
for (x <- xs)
for (y <- ys; x = foo(y))
for ((x, _) <- zs)
for ((_, y @ Option(_)) <- ws)

// Method arguments
def foo(x: Int) = 
def foo(y: Int)(implicit x: Foo) =
class Foo(x: Int)
class Foo(val x: Int)
class Foo(var x: Int)
case class Foo(x: Int)
case class Foo(var x: Int)
//在块中
val x=5
变量x=5
延迟值x=5
def x=5
对象x{val value=5}
val MyCaseClass(x,u)=一个MyCaseClass类
val MyCaseClass(u,另一个(x))=foo
val MyCaseClass(x@other())=foo
//功能
m、 映射(x=>bar(x))
m、 映射((x:Int)=>bar(x))
//分解结构的函数
m、 map{case y if foo(y)=>baz;case x=>bar(x)}
m、 映射{case选项(x)=>bar(x)}
m、 映射{case选项(列表(x))=>bar(x)}
m、 映射{case选项(x@List())=>foo(x)}
//带/不带分解结构的部分函数
m、 收集{case x=>bar(x)}
m、 收集{case选项(列表(x))=>bar(x)}
m、 收集{case选项(x@List())=>foo(x)}
//理解

对于(x这里列出了我所知道的可能不同的一切;
x
是创建的变量:

// Within a block
val x = 5
var x = 5
lazy val x = 5
def x = 5
object x { val value = 5 }
val MyCaseClass(x, _) = oneOfMyCaseClasses
val MyCaseClass(_, Another(x)) = foo
val MyCaseClass(_, x @ Another(_)) = foo

// Functions
m.map( x => bar(x) )
m.map( (x: Int) => bar(x) )

// Functions that destructure
m.map{ case y if foo(y) => baz; case x => bar(x) }
m.map{ case Option(x) => bar(x) }
m.map{ case Option(List(x)) => bar(x) }
m.map{ case Option(x @ List(_)) => foo(x) }

// Partial functions with/without destructuring
m.collect{ case x => bar(x) }
m.collect{ case Option(List(x)) => bar(x) }
m.collect{ case Option(x @ List(_)) => foo(x) }

// For comprehensions
for (x <- xs)
for (y <- ys; x = foo(y))
for ((x, _) <- zs)
for ((_, y @ Option(_)) <- ws)

// Method arguments
def foo(x: Int) = 
def foo(y: Int)(implicit x: Foo) =
class Foo(x: Int)
class Foo(val x: Int)
class Foo(var x: Int)
case class Foo(x: Int)
case class Foo(var x: Int)
//在块中
val x=5
变量x=5
延迟值x=5
def x=5
对象x{val value=5}
val MyCaseClass(x,u)=一个MyCaseClass类
val MyCaseClass(u,另一个(x))=foo
val MyCaseClass(x@other())=foo
//功能
m、 映射(x=>bar(x))
m、 映射((x:Int)=>bar(x))
//分解结构的函数
m、 map{case y if foo(y)=>baz;case x=>bar(x)}
m、 映射{case选项(x)=>bar(x)}
m、 映射{case选项(列表(x))=>bar(x)}
m、 映射{case选项(x@List())=>foo(x)}
//带/不带分解结构的部分函数
m、 收集{case x=>bar(x)}
m、 收集{case选项(列表(x))=>bar(x)}
m、 收集{case选项(x@List())=>foo(x)}
//理解

对于(x)我不确定的一件事是匿名函数,你可以有一个带有
def
的函数,里面有一些匿名函数,我想这些函数也应该包含新的变量。我不包括用
def
定义的函数。但是我包括函数参数(也是匿名的)或类型为
function n
的值,例如
val-fun=(x:Int)=>x
。据我所知,我可以像对待普通变量一样对待它们。有一件事我不确定,那就是匿名函数,你可以有一个带有
def
的函数,里面有一些匿名函数,我想这些函数也应该算上新变量。我不包括用
def
定义的函数。我不知道我们包括函数参数(也是匿名的)或
function n
类型的值,例如
val-fun=(x:Int)=>x
。据我所知,我可以用处理普通变量的相同方式来处理这些参数。因为问题是宏如何查看valdef,那么
中的
x$1
临时变量(1到10)如何?toList indexOf(from=1,elem=5)
或类似的
(1到10)。toList indexOf(uuxInt,1)
。好吧,有很多工作要做!太棒了!宏时间('x$1'等)没有关系。类似的,
\uu
被排除在外。基本上,我只关心在源代码中也可见并且可以被某个术语引用的变量。因为问题是关于查看valdef的宏,那么
(1到10)中的
x$1
临时变量如何?toList indexOf(from=1,elem=5)
或类似的
(1到10).toList indexOf(u1;:Int,1)
。好吧,有很多工作要做!啊!宏临时变量('x$1')没有关系。类似的,
1;
被排除在外。基本上我只关心在源代码中也可见并且可以被某个术语引用的变量。