我最初遇到下面的行爲,其中當一個案例類沒有伴隨對象時,正常工作的代碼在定義該伴隨對象時將無法編譯。我可以通過調用Bar.apply _
而不是簡單的Bar
來修復它,但我仍然不明白爲什麼這是必要的。案例類具有隨播對象時會發生什麼變化?
我試圖使用https://github.com/marklister/product-collections的產品集合庫,它會很高興地將CSV解析爲一個案例類,但只有在該案例類沒有伴隨對象的情況下。這是令人費解的,尤其是因爲the documentation suggests,伴侶對象是放置額外隱式轉換器的理想場所。
我看到相同的行爲,如果我以相反的順序定義案例類和對象,並且我在REPL中看到的內容與我的IDE相匹配。
將伴隨對象定義爲案例類的作用是什麼?我期望它完全不是什麼,因爲伴侶對象(帶有apply和unapply方法)已經被定義爲一個case類。
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> import com.github.marklister.collections.io.CsvParser
import com.github.marklister.collections.io.CsvParser
scala> case class Foo(i: Int, d: Double, s: String)
defined class Foo
scala> case class Bar(i: Int, d: Double, s: String); object Bar {}
defined class Bar
defined object Bar
scala> CsvParser(Foo).parse(new java.io.StringReader("1, 2.4,\"hi\"\n3, 8.9,\"bye\""))
res0: Seq[Foo] = List(Foo(1,2.4,hi), Foo(3,8.9,bye))
scala> CsvParser(Bar).parse(new java.io.StringReader("1, 2.4,\"hi\"\n3, 8.9,\"bye\""))
<console>:14: error: overloaded method value apply with alternatives:
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) => F)com.github.marklister.collections.io.CsvParserF22[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,T21,T22,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) => F)com.github.marklister.collections.io.CsvParserF21[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,T21,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) => F)com.github.marklister.collections.io.CsvParserF20[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) => F)com.github.marklister.collections.io.CsvParserF19[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) => F)com.github.marklister.collections.io.CsvParserF18[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) => F)com.github.marklister.collections.io.CsvParserF17[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) => F)com.github.marklister.collections.io.CsvParserF16[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) => F)com.github.marklister.collections.io.CsvParserF15[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) => F)com.github.marklister.collections.io.CsvParserF14[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) => F)com.github.marklister.collections.io.CsvParserF13[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) => F)com.github.marklister.collections.io.CsvParserF12[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) => F)com.github.marklister.collections.io.CsvParserF11[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) => F)com.github.marklister.collections.io.CsvParserF10[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, T9, F](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9) => F)com.github.marklister.collections.io.CsvParserF9[T1,T2,T3,T4,T5,T6,T7,T8,T9,F] <and>
[T1, T2, T3, T4, T5, T6, T7, T8, F](f: (T1, T2, T3, T4, T5, T6, T7, T8) => F)com.github.marklister.collections.io.CsvParserF8[T1,T2,T3,T4,T5,T6,T7,T8,F] <and>
[T1, T2, T3, T4, T5, T6, T7, F](f: (T1, T2, T3, T4, T5, T6, T7) => F)com.github.marklister.collections.io.CsvParserF7[T1,T2,T3,T4,T5,T6,T7,F] <and>
[T1, T2, T3, T4, T5, T6, F](f: (T1, T2, T3, T4, T5, T6) => F)com.github.marklister.collections.io.CsvParserF6[T1,T2,T3,T4,T5,T6,F] <and>
[T1, T2, T3, T4, T5, F](f: (T1, T2, T3, T4, T5) => F)com.github.marklister.collections.io.CsvParserF5[T1,T2,T3,T4,T5,F] <and>
[T1, T2, T3, T4, F](f: (T1, T2, T3, T4) => F)com.github.marklister.collections.io.CsvParserF4[T1,T2,T3,T4,F] <and>
[T1, T2, T3, F](f: (T1, T2, T3) => F)com.github.marklister.collections.io.CsvParserF3[T1,T2,T3,F] <and>
[T1, T2, F](f: (T1, T2) => F)com.github.marklister.collections.io.CsvParserF2[T1,T2,F] <and>
[T1, F](f: T1 => F)com.github.marklister.collections.io.CsvParserF1[T1,F]
cannot be applied to (Bar.type)
CsvParser(Bar).parse(new java.io.StringReader("1, 2.4,\"hi\"\n3, 8.9,\"bye\""))
^
scala> CsvParser(Bar.apply _).parse(new java.io.StringReader("1, 2.4,\"hi\"\n3, 8.9,\"bye\""))
res1: Seq[Bar] = List(Bar(1,2.4,hi), Bar(3,8.9,bye))
這可能是相關的:https://stackoverflow.com/questions/5827510/how-to-override-apply-in-a-case-class-companion – childofsoong