まず、Scalaについての基本的なことを説明します。 Scalaで
、あなたはそのclass
のインスタンスを作成するために使用されるスカラ座に与えられた青写真としてclass
と考えることができ
scala> class Demo(a: String, b: Int) {
| def stringify: String = a + " :: " + b
| }
// defined class Demo
、次のようなクラスを定義します。ここでは、class Demo
のすべてのインスタンスは、String
とb
となる2つのプロパティ-Int
と1つのメソッド-を持ち、String
を返します。ここ
scala> val demo1 = new Demo("demo1", 1)
// demo1: Demo = [email protected]
scala> demo1.getClass
// res0: Class[_ <: Demo] = class Demo
demo1
はclass
Demo
のインスタンスであり、type
Demo
を有しています。
また、Scalaには特別に生成された内部クラスのインスタンスであるobject
という概念があります。
ここ
scala> object OtherDemo {
| val a: Int = 10
| }
// defined object OtherDemo
scala> DemoObject.getClass
// res2: Class[_ <: OtherDemo.type] = class OtherDemo$
OtherDemo
は、特別に生成class
OtherDemo$
の唯一のインスタンスであるとtype
OtherDemo.type
を有しているであろう。
そしてcase class
これはclass
AnotherDemo
だけでなく、我々はコンパニオンオブジェクトを呼び出すobject
AnotherDemo
ないだけに作成されますScalaの
scala> case class AnotherDemo(a: Int)
// defined class AnotherDemo
です。これは私たちがclass
AnotherDemo
のcompanion object
としてこのobject
AnotherDemo
を呼び出す
class AnotherDemo(a: Int)
object AnotherDemo {
def apply(a: Int): AnotherDemo = new AnotherDemo(a)
def unapply(anotherDemo: AnotherDemo): Option[Int] = Some(anotherDemo.a)
// And many more utility functions
}
、と等価です。
私たちはスカラ座では、あなたのクラス名は大文字で始まる必要があり、二つの方法、また
// By using new keyword, as we can do for any class
scala> val anotherDemo1 = new AnotherDemo(1)
// anotherDemo1: AnotherDemo = AnotherDemo(1)
// Or we can use `apply` method provided by companion object
scala> val anotherDemo2 = AnotherDemo(2)
// anotherDemo2: AnotherDemo = AnotherDemo(2)
scala> anotherDemo1.getClass
// res6: Class[_ <: AnotherDemo] = class AnotherDemo
scala> anotherDemo2.getClass
// res7: Class[_ <: AnotherDemo] = class AnotherDemo
scala> AnotherDemo.getClass
// res8: Class[_ <: AnotherDemo.type] = class AnotherDemo$
にAnotherDemo
のインスタンスを作成することができます。これにより、小文字で始まるインスタンス変数と簡単に区別することができます。これは混乱を避けるのに役立ちます。
ここでa: String
であり、a: string
ではないとします。
scala> case class Test(
| a: String,
| b: String,
| c: Int,
| d: Int
| )
// defined class Test
さて、あなたが書いたときに、
scala> var temp = List(Test("lol","lel",1,2))
// temp: List[Test] = List(Test(lol,lel,1,2))
それは実際に相当し、
var temp = List.apply(Test.apply("lol","lel",1,2))
あるいは、
val test1 = Test.apply("lol","lel",1,2)
var temp = List.apply(test1)
Test.apply
でTest
がヨーヨーではありませんur class Test
でも、companion object Test
です。そしてTest.apply
を呼び出すと、最終的List
Test
のこのインスタンスを含むList[Test]
type
のを取得するためにList.apply
に渡されるclass Test
のインスタンスを返します。
しかし、ときにこれを書き、
scala> var total = List(Test)
// total: List[Test.type] = List(Test)
あなたはTest
のcompanion object
を含むタイプList[Test.type]
のList
を作成しています。 total: List[Test.type]
一部の
フォーカス...これはtotal
は、それがtype
List[Test.type]
のvalue/instance
を指すようになるでしょうし、何か他のものを指すように拒否することを意味しますtype
List[Test.type]
のvariable
であることを意味します。
今...あなたはこれをやろうとしている、と等価である
total = total ::: temp
、実際には
val x = total ::: temp
total = x
、
val x = temp.:::(total)
total = x
は今、このval x = total ::: temp
を見て、
scala> val x = total ::: temp
// x: List[Serializable] = List(Test, Test(lol,lel,1,2))
ご覧ください... x
はList[Serializable]
です。あなたがtotal = x
をしようとするので、あなたは、エラー以下のtotal
がList[Test.type]
を必要ですが、あなたはそれをList[Serializable]
を与えていることを意味
scala> total = x
// <console>:13: error: type mismatch;
// found : List[Serializable]
// required: List[Test.type]
// total = x
// ^
を取得します。