我想创建一个特殊的计算器.我认为这case class对操作来说是一个好主意:
sealed class Expr
case class add(op1:Int, op2:Int) extends Expr
case class sub(op1:Int, op2:Int) extends Expr
case class mul(op1:Int, op2:Int) extends Expr
case class div(op1:Int, op2:Int) extends Expr
case class sqrt(op:Int) extends Expr
case class neg(op:Int) extends Expr
/* ... */
Run Code Online (Sandbox Code Playgroud)
现在我可以使用match-case来解析输入.也许,我也应该使用traits(如:trait Distributivity,trait Commutativity等等),那是更多钞票?这是一个好主意吗?
我想测试我的case类构造函数的参数,如果它们在某些测试中失败则抛出异常.当我尝试编写自己的apply方法时,编译器抱怨了(多个'apply'方法.
我想我可以把它变成一个非案例类,并自己做apply/unapply构造函数字段,但我希望不要这样做.
谢谢
我scalap用来读出一些案例类的字段名称(如本问题所述).案例类和scalap用于分析它们的代码都已编译并放入类路径上的jar文件中.
现在我想运行一个使用这段代码的脚本,所以我按照说明操作,想出了类似的东西
::#!
@echo off
call scala -classpath *;./libs/* %0 %*
goto :eof
::!#
//Code relying on pre-compiled code that uses scalap
Run Code Online (Sandbox Code Playgroud)
这不起作用:
java.lang.ClassCastException:scala.None $无法转换为scala.tools.nsc.interpreter.ByteCode $ .caseParamNamesForPath(ByteCode.scala:45)scala.tools.nsc.interpreter.ProductCompletion.caseNames上的scala.Option( ProductComple tion.scala:22)
但是,当我编译所有内容时,代码工作得很好.我玩过其他scala类似的选项-savecompiled,但这没有帮助.这是一个错误,还是原则上不能起作用?(如果是这样,有人可以解释为什么不这样做吗?正如我所说,将分析应分析的案例类scalap.)
注意:我使用Scala 2.9.1-1.
编辑
这是我本来想要做的事情(提供一种创建案例类的多个实例的简单方法):
//This is pre-compiled:
import scala.tools.nsc.interpreter.ProductCompletion
//...
trait MyFactoryTrait[T <: MyFactoryTrait[T] with Product] {
this: T =>
private[this] val copyMethod = this.getClass.getMethods.find(x => x.getName == "copy").get
lazy val productCompletion = new ProductCompletion(this)
/** …Run Code Online (Sandbox Code Playgroud) 我看到有对Option类型的支持,但是自定义案例类呢?
我有点想这样做:
result match {
case SuccessCase(values) => {
values.foo should be ("bar")
}
case FailureCase => // should fail test, but how to say this in ScalaTest?
}
Run Code Online (Sandbox Code Playgroud) 所以我有这样的事情:
abstract class Term
case class App(f:Term,x:Term) extends Term
case class Var(s:String) extends Term
case class Amb(a:Term, b:Term) extends Term //ambiguity
Run Code Online (Sandbox Code Playgroud)
一个术语可能如下所示:
App(Var(f),Amb(Var(x),Amb(Var(y),Var(z))))
Run Code Online (Sandbox Code Playgroud)
所以我需要的是Amb类所指示的所有变体.这用于表示模糊的解析林,我想键入检查每个可能的变体并选择正确的变体.在这个例子中我需要:
App(Var(f),Var(x))
App(Var(f),Var(y))
App(Var(f),Var(z))
Run Code Online (Sandbox Code Playgroud)
什么是在scala中创建这些变体的最佳方法?效率会很好,但并不是真正的要求.如果可能的话,我喜欢不使用反射.
让我们假设我们有一个共同的特质模型.
trait Model {
def id: String
def updated: Date
}
Run Code Online (Sandbox Code Playgroud)
我们有2个案例类来扩展这个特性.
case class C1(id: String, updated: Date, foo: String) extends Model
case class C2(id: String, updated: Date, bar: Int) extends Model
Run Code Online (Sandbox Code Playgroud)
是否可以编写如下所示的实用程序函数,该函数将Model作为参数并返回带有更新字段的更新值的副本?
object Model {
def update[T <: Model](model: T): T = {
model.copy(updated = new Date) // This code does not compile.
}
}
Run Code Online (Sandbox Code Playgroud) case class假设您有以下代码,我在将具有> 22列的表专门映射到a时遇到问题
import slick.driver.PostgresDriver
import scala.slick.collection.heterogenous._
import syntax._
import shapeless.Generic
case class TwentyThreeCaseClass(
val id:Option[Long],
val one:String,
val two:String,
val three:String,
val four:String,
val five:String,
val six:String,
val seven:String,
val eight:String,
val nine:String,
val ten:String,
val eleven:String,
val twelve:String,
val thirteen:String,
val fourteen:String,
val fifteen:String,
val sixteen:String,
val seventeen:String,
val eighteen:String,
val nineteen:String,
val twenty:String,
val twentyOne:String,
val twentyTwo:String,
val twentyThree:String,
val twentyFour:String
)
class TwentyThreeTable(tag:Tag) extends Table[TwentyThreeCaseClass](tag,"twenty_three_table") {
def id = column[Long]("id",O.PrimaryKey,O.AutoInc)
def one = column[String]("one")
def …Run Code Online (Sandbox Code Playgroud) 假设我们有以下案例类:
case class CasePerson(firstName: String)
Run Code Online (Sandbox Code Playgroud)
我们还为它定义了一个伴侣对象:
object CasePerson {
def apply() = new CasePerson( "XYZ" )
}
Run Code Online (Sandbox Code Playgroud)
请注意,在上面的示例中,我使用apply方法显式定义了伴随对象,而没有定义默认的apply方法:
// This "default" apply has the same argument as the primary constructor of the case class
def apply(firstName : String) = new CasePerson(firstName)
Run Code Online (Sandbox Code Playgroud)
问:那么Scala在哪里获得"默认"适用?我在这里明确定义了伴随对象,没有默认的apply,编译器仍然知道如何执行:
val casePerson = CasePerson("PQR")
Run Code Online (Sandbox Code Playgroud)
这是如何运作的?
"案例是" 特定情况的一个例子; 发生事情的一个例子 '.
所以我的问题是 - 为什么Scala'case'类被命名为'case'?有什么意义?为什么它是"案例",而不是"数据"类或其他什么?什么意思是'案例'在那.. ..案例:)
我已经定义了几个用于JSON表示的案例类,但是由于存在很多嵌套的案例类,因此我不确定我是否正确执行了。诸如spec,meta之类的实体的类型均为JSONObject以及Custom对象本身。
这是我定义的所有类:
case class CustomObject(apiVersion: String,kind: String, metadata: Metadata,spec: Spec,labels: Object,version: String)
case class Metadata(creationTimestamp: String, generation: Int, uid: String,resourceVersion: String,name: String,namespace: String,selfLink: String)
case class Spec(mode: String,image: String,imagePullPolicy: String, mainApplicationFile: String,mainClass: String,deps: Deps,driver: Driver,executor: Executor,subresources: Subresources)
case class Driver(cores: Double,coreLimit: String,memory: String,serviceAccount: String,labels: Labels)
case class Executor(cores: Double,instances: Double,memory: String,labels: Labels)
case class Labels(version: String)
case class Subresources(status: Status)
case class Status()
case class Deps()
Run Code Online (Sandbox Code Playgroud)
这是我需要转换的自定义K8s对象的JSON结构:
{
"apiVersion": "sparkoperator.k8s.io/v1alpha1",
"kind": "SparkApplication",
"metadata": {
"creationTimestamp": "2019-01-11T15:58:45Z",
"generation": 1,
"name": "spark-example", …Run Code Online (Sandbox Code Playgroud) case-class ×10
scala ×10
shapeless ×2
json ×1
kubernetes ×1
reflection ×1
scalap ×1
scalatest ×1
slick ×1
traits ×1