我最初遇到下面的行为,当一个案例类没有一个伴随对象时,代码工作正常,在定义该伴随对象时将无法编译。我可以通过调用Bar.apply _
而不是简单地Bar
来修复它,但我仍然不明白为什么这是必要的。
我正在尝试使用https://github.com/marklister/product-collections中的产品集合库,它很乐意将CSV解析为案例类,但前提是该案例类没有伴随对象。这是令人费解的,特别是因为the documentation suggests伴随对象是放置其他隐式转换器的理想位置。
如果我以相反的顺序定义case类和对象,我会看到相同的行为,我在REPL中看到的与我的IDE匹配。
将伴随对象定义到案例类有什么影响?我希望它完全没有,因为伴随对象(使用apply和unapply方法)已经通过作为案例类来定义。
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))
答案 0 :(得分:2)
案例类Foo
的自动生成的随播广告对象将展开(Int, Double, String) => Foo
。如果您自己为案例类Bar
定义伴随对象,编译器将不再自动添加该继承关系。
有关详细信息,请查看SI-3664。