typelevel / algebra

Experimental project to lay out basic algebra type classes
https://typelevel.org/algebra/
Other
378 stars 69 forks source link

cast class exception using algebra with algebird #179

Open johnynek opened 8 years ago

johnynek commented 8 years ago
[info]   java.lang.ClassCastException: com.twitter.algebird.IntRing$ cannot be cast to cats.kernel.CommutativeGroup
[info]   at com.twitter.algebird.IntRing$.additive(Ring.scala:66)
[info]   at com.twitter.algebird.AdjoinedUnitRing.<init>(AdjoinedUnitRing.scala:40)
[info]   at com.twitter.algebird.AdjoinedUnit$.ring(AdjoinedUnitRing.scala:32)
[info]   at com.twitter.algebird.AdjoinedRingSpecification.<init>(AdJoinedUnitRing.scala:40)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[info]   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[info]   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
[info]   at java.lang.Class.newInstance(Class.java:442)
[info]   at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:435)

The code compiled, but at runtime it failed. This resulted from treating a Ring[T] as a Rng[T]. I'll try to repro here.

johnynek commented 8 years ago

I fixed it by making algebra Ring extend CommutativeGroup. Note, the code compiled, but I got a class cast exception at runtime.

https://github.com/twitter/algebird/pull/523/files#diff-7347f7f93e4781e9ba09b718ce5d981cR47

Seems like some kind of issue with the inheritance we have set up, and somehow scala thinks things are fine at runtime, but makes some unsafe casts.

It's a little concerning.