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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.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
&引用;正向引用扩展到定义值exp";将抽象类与Scala教程的案例类一起使用时_Scala - Fatal编程技术网

&引用;正向引用扩展到定义值exp";将抽象类与Scala教程的案例类一起使用时

&引用;正向引用扩展到定义值exp";将抽象类与Scala教程的案例类一起使用时,scala,Scala,我正在做一个关于Scala()的教程,这里有一个如何使用case类和模式匹配的示例 我正在使用IDE IntelliJ思想 我试过这个例子,我有一个抽象的类树 类Sum、Var和Const 在本教程中,实现了eval和derive方法 还有一个显示类功能的main方法 还有:在编译之前,您需要将环境类型和eval、派生以及main方法包装到Calc对象中。执行这个程序,我们得到了预期的输出 我将类型Environment和方法eval、派生和main放入一个名为Calc的对象中。 但还有Sum、

我正在做一个关于Scala()的教程,这里有一个如何使用case类和模式匹配的示例

我正在使用IDE IntelliJ思想

我试过这个例子,我有一个抽象的类树 类Sum、Var和Const

在本教程中,实现了eval和derive方法

还有一个显示类功能的main方法

还有:在编译之前,您需要将环境类型和eval、派生以及main方法包装到Calc对象中。执行这个程序,我们得到了预期的输出

我将类型Environment和方法eval、派生和main放入一个名为Calc的对象中。 但还有Sum、Var和Const没有解决。所以我在变量前面加了exp.(exp.Sum,exp.Var,exp.Const) exp.是树类的变量

根据教程,在该case类前面没有exp,如果在main方法中调用该case类之前省略exp,我的IDE将无法找到它们

当我放置exp.(树对象的变量)尝试访问案例类时,出现错误消息“Forward reference Extended over Definition of value exp”

我做错了什么

本教程没有说明如何正确访问抽象类的case类,并且本教程的代码(main方法)不起作用

抽象类是:

抽象类树
case类Sum(l:Tree,r:Tree)扩展了Tree
case类Var(n:String)扩展了树
case类Const(v:Int)扩展了树
环境和评估方法为:

type Environment=String=>Int
def eval(t:Tree,env:Environment):Int=t匹配{
案例总和(l,r)=>评估(l,环境)+评估(r,环境)
案例变量(n)=>环境变量(n)
案例常数(v)=>v
}
推导方法为:

def派生(t:Tree,v:String):Tree=t匹配{
案例和(l,r)=>和(推导(l,v),推导(r,v))
案例变量(n)如果(v==n)=>Const(1)
大小写=>Const(0)
}
主要方法是:

def main(args:Array[String]):单位={
val exp:Tree=Sum(Sum(Var(“x”)、Var(“x”)、Sum(Const(7)、Var(“y”))
val env:Environment={case“x”=>5 case“y”=>7}
println(“表达式:”+exp)
println(“x=5,y=7的评估:+eval(exp,env))
println(“相对于x的导数:\n”+派生(exp,“x”))
println(“相对于y的导数:\n”+派生(exp,“y”))
}
整个代码:


abstract class Tree {

  case class Sum(l: Tree, r: Tree) extends Tree
  case class Var(n: String) extends Tree
  case class Const(v: Int) extends Tree

  type Environment = String => Int

  def eval(t: Tree, env: Environment): Int = t match {
    case Sum(l, r) => eval(l, env) + eval(r, env)
    case Var(n)    => env(n)
    case Const(v)  => v
  }

  def derive(t: Tree, v: String): Tree = t match {
    case Sum(l, r) => Sum(derive(l,v),derive(r,v))
    case Var(n) if (v == n) => Const(1)
    case _ => Const(0)
  }

}

object Calc {



  def main(args: Array[String]): Unit = {
    val exp: Tree = Sum(Sum(Var("x"),Var("x")),Sum(exp.Const(7),Var("y")))
    val env: Environment = {
      case "x" => 5
      case "y" => 7
    }

    println("Ausdruck: " + exp)
    println("Auswertung mit x=5, y=7: " + exp.eval(exp, env))
    println("Ableitung von x:\n " + exp.derive(exp, "x"))
    println("Ableitung von y:\n " + exp.derive(exp, "y"))

  }

}

在这里,IDE无法找到Sum、Var和环境。如果我写经验和 和exp.Var,则会出现另一条错误消息

我不明白如何正确使用该教程的示例。
如何使其工作?

简单的解决方案是将
代码放入对象中并导入它:

object Expr {
  trait Tree
  case class Sum(l: Tree, r: Tree) extends Tree
  case class Var(n: String) extends Tree
  case class Const(v: Int) extends Tree

  type Environment = String => Int

  def eval(t: Tree, env: Environment): Int = t match {
    case Sum(l, r) => eval(l, env) + eval(r, env)
    case Var(n)    => env(n)
    case Const(v)  => v
  }

  def derive(t: Tree, v: String): Tree = t match {
    case Sum(l, r) => Sum(derive(l,v),derive(r,v))
    case Var(n) if (v == n) => Const(1)
    case _ => Const(0)
  }
}

object Calc {
  import Expr._

  def main(args: Array[String]): Unit = {
    val exp: Tree = Sum(Sum(Var("x"),Var("x")),Sum(Const(7),Var("y")))
    val env: Environment = {
      case "x" => 5
      case "y" => 7
    }

    println("Ausdruck: " + exp)
    println("Auswertung mit x=5, y=7: " + eval(exp, env))
    println("Ableitung von x:\n " + derive(exp, "x"))
    println("Ableitung von y:\n " + derive(exp, "y"))
  }
}
但是,将适当的方法直接添加到每个
案例类
,而不是在
eval
派生
中取消它们的链接,这样会更简洁:

object Expr {
  type Environment = String => Int

  trait Tree {
    def eval(env: Environment): Int
    def derive(v: String): Tree
  }
  case class Sum(l: Tree, r: Tree) extends Tree {
    def eval(env: Environment): Int = l.eval(env) + r.eval(env)
    def derive(v: String): Tree = Sum(l.derive(v), r.derive(v))
  }
  case class Var(n: String) extends Tree {
    def eval(env: Environment): Int = env(n)
    def derive(v: String): Tree = if (n == v) Const(1) else Const(0)
  }
  case class Const(v: Int) extends Tree{
    def eval(env: Environment): Int = v
    def derive(v: String): Tree = Const(0)
  }
}

object Calc {
  import Expr._

  def main(args: Array[String]): Unit = {
    val exp: Tree = Sum(Sum(Var("x"),Var("x")),Sum(Const(7),Var("y")))
    val env: Environment = {
      case "x" => 5
      case "y" => 7
    }

    println("Ausdruck: " + exp)
    println("Auswertung mit x=5, y=7: " + exp.eval(env))
    println("Ableitung von x:\n " + exp.derive("x"))
    println("Ableitung von y:\n " + exp.derive("y"))
  }
}

此代码编译正常,因此您需要显示不起作用的代码,包括包含的对象。首先在抽象类树之外声明case类,最后将树声明为trait而不是抽象类。谢谢您的解决方案。可能是教程中的错误,案例类位于抽象类中,而不是对象中,并且使用树作为特征。