Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when projecting a scalacheck Arbitrary Coproduct #187

Open
juanjovazquez opened this issue Feb 28, 2018 · 1 comment
Open

Error when projecting a scalacheck Arbitrary Coproduct #187

juanjovazquez opened this issue Feb 28, 2018 · 1 comment

Comments

@juanjovazquez
Copy link
Contributor

A java.lang.ClassCastException is raised when projecting a sample Coproduct obtained through a scalacheck Arbitrary. Example:

import iota.{ Cop, TNil }
import iota.TList.::
import iota.scalacheck._
import org.scalacheck.{ Arbitrary, Gen }

object IotaScalacheck {

  object Monday
  object Tuesday
  object Wednesday
  object Thursday
  object Friday

  type Weekday = Cop[
    Monday.type ::
    Tuesday.type ::
    Wednesday.type ::
    Thursday.type ::
    Friday.type ::
    TNil
  ]

  implicit val arbMonday    = Arbitrary(Gen.const(Monday))
  implicit val arbTuesday   = Arbitrary(Gen.const(Tuesday))
  implicit val arbWednesday = Arbitrary(Gen.const(Wednesday))
  implicit val arbThursday  = Arbitrary(Gen.const(Thursday))
  implicit val arbFriday    = Arbitrary(Gen.const(Friday))

  def main(args: Array[String]): Unit = {
    val weekday: Weekday = implicitly[Arbitrary[Weekday]].arbitrary.sample.get

    val MondayInject    = Cop.Inject[Monday.type, Weekday]
    val TuesdayInject   = Cop.Inject[Tuesday.type, Weekday]
    val WednesdayInject = Cop.Inject[Wednesday.type, Weekday]
    val ThursdayInject  = Cop.Inject[Thursday.type, Weekday]
    val FridayInject    = Cop.Inject[Friday.type, Weekday]

    weekday match { // runtime error! java.lang.ClassCastException: org.scalacheck.ArbitraryLowPriority$$anon$1 cannot be cast to IotaScalacheck$Thursday$
      case MondayInject(day)    => println(day)
      case TuesdayInject(day)   => println(day)
      case WednesdayInject(day) => println(day)
      case ThursdayInject(day)  => println(day)
      case MondayInject(day)    => println(day)
      case FridayInject(day)    => println(day)
    }
  }
}
@andyscott
Copy link
Member

I think this is a mistake on my part:

Cop.unsafeApply(n, gens.underlying.values(n))))

gens should be renamed to arbitraries then unsafe untyped line needs to be adjusted to get the Gen by calling .arbitrary on the arbitrary instance. Ideally we can add some tests to ensure this works correctly.

I can take a stab at this later this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants