NonEmptyList with shapeless

59 views
Skip to first unread message

Andrey Kuznetsov

unread,
Mar 7, 2015, 3:52:27 AM3/7/15
to shapel...@googlegroups.com
Wiki says that sized besides fixed size "can also verify more complex relationships".
Is it possible to use shapeless features for checking list on non-emptiness like scalaz's NonEmptyList?

Edmondo Porcu

unread,
Mar 7, 2015, 9:32:44 AM3/7/15
to shapel...@googlegroups.com
A typical way to solve this scenarios in shapeless is to provide compile-time proof.
- You create a class which acts as a witness that the proof is verified
- You provide the implicits only for certain cases.

@implicitNotFound("Cannot prove that ${L} is not empty")
trait NonEmptyConstraint[L <: HList]

object NonEmptyConstraint {

  implicit def oneElementNonEmptyHListConstraint[T]: NonEmptyConstraint[T::HNil] = new NonEmptyConstraint[T::HNil]{}

  implicit def hlistNonEmptyConstraint[T,H<:HList](implicit nec:NonEmptyConstraint[H]): NonEmptyConstraint[T::H] = new NonEmptyConstraint[T::H]{}

}

def a[A<:Liist](a:A)(implicit nec:NonEmptyConstraint[A]) 




2015-03-07 0:11 GMT+00:00 Andrey Kuznetsov <fe...@loathing.in>:
Wiki says that sized besides fixed size "can also verify more complex relationships".
Is it possible to use shapeless features for checking list on non-emptiness like scalaz's NonEmptyList?

--
You received this message because you are subscribed to the Google Groups "Shapeless Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shapeless-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages