Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@milessabin
milessabin / singleton-only.scala
Created June 6, 2016 11:04
Scala type which can only be extended by an object, not by a non-abstract type ...
scala> class Foo { self: Singleton => }
defined class Foo
scala> class Bar extends Foo
<console>:12: error: illegal inheritance;
self-type Bar does not conform to Foo's selftype Foo with Singleton
class Bar extends Foo
^
scala> object Bar extends Foo
import scala.language.higherKinds
object cats {
trait Show[A] {
def apply(x: A): String
}
object Show {
implicit def stringShow: Show[String] = new Show[String] {
def apply(x: String): String = x
@milessabin
milessabin / blah.scala
Created May 19, 2016 13:32
Doing something useful with bare singleton types and no macros ...
scala> trait Label[T] { val value: Boolean }
defined trait Label
scala> def mkInstance(s: String, v: Boolean): Label[s.type] = new Label[s.type] { val value = v }
mkInstance: (s: String, v: Boolean)Label[s.type]
scala> implicit def lTrue = mkInstance("True", true)
lTrue: Label[String("True")]
scala> implicit def lFalse = mkInstance("False", false)
@milessabin
milessabin / shapeless-session.txt
Created April 1, 2016 12:29
shapeless on the Ammonite REPL with no dependencies other than an installed JDK. Many thanks to @przemekpokrywka for the idea, @alxarchambault for Coursier and @li_haoyi for Ammonite.
miles@frege:~$ ./shapeless.sh
Loading...
Welcome to the Ammonite Repl 0.5.2
(Scala 2.11.7 Java 1.8.0_51)
@ val l = 23 :: "foo" :: true :: HNil
l: Int :: String :: Boolean :: HNil = ::(23, ::("foo", ::(true, HNil)))
@
@milessabin
milessabin / dec.scala
Created March 21, 2016 10:31
Idris's Dec in Scala
sealed trait Dec[+T]
final case class Yes[T](t: T) extends Dec[T]
final case object No extends Dec[Nothing]
object Dec extends Dec0 {
implicit def yes[T](implicit t: T): Dec[T] = Yes(t)
}
trait Dec0 {
implicit def no[T]: Dec[T] = No
```scala
object Foo {
bar
}
```
[info] published shapeless_sjs0.6_2.12.0-M3 to https://oss.sonatype.org/service/local/staging/deploy/maven2/com/chuusai/shapeless_sjs0.6_2.12.0-M3/2.3.0-RC4/shapeless_sjs0.6_2.12.0-M3-2.3.0-RC4.jar
[success] Total time: 106 s, completed 23-Feb-2016 16:54:42
[info] Setting scala version to 2.11.7
[info] Set current project to root (in build file:/home/miles/projects/shapeless/)
[info] Setting version to '2.3.0-SNAPSHOT'.
[info] Reapplying settings...
[info] Set current project to root (in build file:/home/miles/projects/shapeless/)
[info] [master f0e0f6c] Setting version to 2.3.0-SNAPSHOT
[info] 1 file changed, 1 insertion(+), 1 deletion(-)
Push changes to the remote repository (y/n)? [y] y
@milessabin
milessabin / tuplegeneric.scala
Last active March 13, 2023 20:27
Convert (small enough) case classes to and from tuples using shapeless ...
import shapeless._
import ops.hlist.Tupler
trait TupleGeneric[C <: Product] extends Serializable {
type Repr <: Product
def to(t : C) : Repr
def from(r : Repr) : C
}
import shapeless._
import ops._
import coproduct._
object coproducttest {
type U = Int :+: String :+: CNil
type V = Double :+: List[Int] :+: CNil
import shapeless._, ops.function._, ops.hlist._
trait Bar {
trait Wrapper { type T }
case class A[U]() extends Wrapper { type T = U }
trait Unwrapper {
type Args <: HList
type F[R] = Args => R