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

Compile-time refinement for cons #575

Open
steinybot opened this issue Oct 15, 2018 · 1 comment
Open

Compile-time refinement for cons #575

steinybot opened this issue Oct 15, 2018 · 1 comment

Comments

@steinybot
Copy link
Contributor

steinybot commented Oct 15, 2018

It would be cool if this worked:

type NonEmptySeq[A] = immutable.Seq[A] Refined NonEmpty

object NonEmptySeq {
  def apply[A]: RefinedTypeOps[NonEmptySeq[A], immutable.Seq[A]] = {
    new RefinedTypeOps[NonEmptySeq[A], immutable.Seq[A]]
  }
}

val a = "refined"
val b = "types" :: "are" :: "cool!"
NonEmptySeq[String](a :: b)
// or at least
NonEmptySeq[String](::(a, b))
@fthomas
Copy link
Owner

fthomas commented Feb 3, 2019

This seems to work:

object collection {
  type NonEmptyList[A] = List[A] Refined NonEmpty

  object NonEmptyList {
    class NonEmptyListOps[A] extends RefinedTypeOps[NonEmptyList[A], List[A]] {
      def fromCons(cons: ::[A]): NonEmptyList[A] =
        Refined.unsafeApply(cons)
    }

    def apply[A]: NonEmptyListOps[A] = new NonEmptyListOps[A]
  }
}
scala> NonEmptyList[String].fromCons(::("hello", Nil))
res5: NonEmptyList[String] = List(hello)

I'm open to adding this to the types package.

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