Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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 - Fatal编程技术网

Scala程序变量创建

Scala程序变量创建,scala,Scala,我对斯卡拉很陌生。我想指定一个字符串作为变量名: val names = Vector("a","b","c") for (name <~ names){ val <xyz> = "test" } 如何编写代码以获得此结果?我首先误解了您的问题 val map = (for (name <- names) yield (name -> "test")).toMap map: scala.collection.immutable.Map[String,String

我对斯卡拉很陌生。我想指定一个字符串作为变量名:

val names = Vector("a","b","c")
for (name <~ names){
 val <xyz> = "test"
}

如何编写代码以获得此结果?

我首先误解了您的问题

val map = (for (name <- names) yield (name -> "test")).toMap
map: scala.collection.immutable.Map[String,String] = Map(a -> test, b -> test, c -> test)
如果您只是搜索一个简短的表单来初始化3个变量,您可以执行以下操作:

val (a, b, c) = ("test", "test", "test")
a: String = test
b: String = test
c: String = test

我首先误解了你的问题

val map = (for (name <- names) yield (name -> "test")).toMap
map: scala.collection.immutable.Map[String,String] = Map(a -> test, b -> test, c -> test)
如果您只是搜索一个简短的表单来初始化3个变量,您可以执行以下操作:

val (a, b, c) = ("test", "test", "test")
a: String = test
b: String = test
c: String = test
此外,您还可以在
Vector
上使用模式匹配,尽管如果您只想创建三个具有相同值的变量,这远远不够优雅。正如UserUnknown指出的,您可以在元组上进行模式匹配:
val(a,b,c)=(“test”,“test”,“test”)

val namesList=列表(“a”、“b”、“c”)
val a::b::c::Nil=for{
名称添加到答案中,您可以在
向量上使用模式匹配,尽管如果您只想创建具有相同值的三个变量,这远不是优雅的。正如用户unknown所指出的,您可以在元组上进行模式匹配:
val(a,b,c)=(“test”,“test”,“test”)

val namesList=列表(“a”、“b”、“c”)
val a::b::c::Nil=for{

nameHm…有一种方法可以使用Scala宏来实现此目的。示例:

object VarrApp extends App {
  // create a Varr annotation, this in the compile time, will automatically expand the names Vector and generate these variables
  @Varr
  val names = Vector("a", "b", "c")
  println(a) // test
  println(b) // test
  println(c) // test
}
为实现这一目标:

1.为宏创建子模块,项目结构如下:

   project:
       macros-submodule/src/main/scala/Varr.scala
       src/main/scala/VarrApp.scala
2.添加Scala元依赖项,与文档一样,添加天堂编译器插件,如:

addCompilerPlugin(
  "org.scalameta" % "paradise" % "3.0.0-M7" cross CrossVersion.full),
并在Scala选项中启用MacroCradise,如:

scalacOptions ++= Seq("-Xplugin-require:macroparadise")
3.实现
Varr
注释,如:

import scala.annotation.StaticAnnotation
import scala.meta._

class Varr extends StaticAnnotation {
  inline def apply(defn: Any): Any = meta {
    defn match {
      case q"val $name = Vector(..$paramss)" => {
        val stats = paramss.map {
          case i: Lit =>
            s"""val ${i.value} = "test"""".parse[Stat].get
        }
        q"""
            ..$stats
         """
      }
      case _ => abort("illegal stats")
    }
  }
}

嗯……有一种方法可以使用Scala宏来实现这一点。例如:

object VarrApp extends App {
  // create a Varr annotation, this in the compile time, will automatically expand the names Vector and generate these variables
  @Varr
  val names = Vector("a", "b", "c")
  println(a) // test
  println(b) // test
  println(c) // test
}
为实现这一目标:

1.为宏创建子模块,项目结构如下:

   project:
       macros-submodule/src/main/scala/Varr.scala
       src/main/scala/VarrApp.scala
2.添加Scala元依赖项,与文档一样,添加天堂编译器插件,如:

addCompilerPlugin(
  "org.scalameta" % "paradise" % "3.0.0-M7" cross CrossVersion.full),
并在Scala选项中启用MacroCradise,如:

scalacOptions ++= Seq("-Xplugin-require:macroparadise")
3.实现
Varr
注释,如:

import scala.annotation.StaticAnnotation
import scala.meta._

class Varr extends StaticAnnotation {
  inline def apply(defn: Any): Any = meta {
    defn match {
      case q"val $name = Vector(..$paramss)" => {
        val stats = paramss.map {
          case i: Lit =>
            s"""val ${i.value} = "test"""".parse[Stat].get
        }
        q"""
            ..$stats
         """
      }
      case _ => abort("illegal stats")
    }
  }
}