Skip to content

Instantly share code, notes, and snippets.

View propensive's full-sized avatar

Jon Pretty propensive

View GitHub Profile
object Unpack:
import scala.quoted._
import scala.compiletime._
private def tupleType(s: String)(using q: Quotes): q.reflect.TypeRepr =
import q.reflect._
val typeParams =
s.map{ c =>
TypeRepr.of(using ConstantType(CharConstant(c)).asType)
}.toList

A short journey through types in Scala

(based on the Jon Pretty's "Advanced Type Mechanics" workshop)

Type

It is a named collection of method signatures. Those method signatures are called properties.

Classes and traits vs types

We should be careful not to confuse classes or traits and types, because they are different things. Every class and trait gives rise to a type with the same name, but in itself is not a type. Many types also exist which do not solely correspond to a trait or a class.

@propensive
propensive / DiffableDerivation.scala
Created December 23, 2018 05:18 — forked from Fristi/DiffableDerivation.scala
specs2 `Diffable` magnolia derivation
import magnolia.{CaseClass, Magnolia, SealedTrait}
import org.specs2.matcher.describe._
import scala.language.experimental.macros
object DiffableDerivation {
/** binds the Magnolia macro to this derivation object */
implicit def genDiffable[T]: Diffable[T] = macro Magnolia.gen[T]
### Keybase proof
I hereby claim:
* I am propensive on github.
* I am propensive (https://keybase.io/propensive) on keybase.
* I have a public key ASArWRtQnt8y3AuDBYD0B3FsCtq6W4pRE0tAl6GhlZMAcAo
@propensive
propensive / derivation_benchmarks
Created November 22, 2017 23:37
Derivation benchmarks
magnolia/one:
00:01.388187 src/foo.scala
00:04.130835 src/magnolia/one.scala
00:01.339720 src/magnolia/one.scala
00:00.893572 src/magnolia/one.scala
00:00.640120 src/magnolia/one.scala
00:00.664193 src/magnolia/one.scala
00:00.608714 src/magnolia/one.scala
00:00.502048 src/magnolia/one.scala
00:00.497764 src/magnolia/one.scala
Welcome to Scala 2.12.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.
scala> case class Foo(x: Int)
defined class Foo
scala> object Bar {
| implicit val foo: Foo = Foo(1)
| object Baz {
| implicit val foo: Foo = Foo(2)
Welcome to Scala 2.12.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.
scala> case class Foo(x: Int)
defined class Foo
scala> implicit def foo: Foo = Foo(1)
foo: Foo
scala> implicitly[Foo]
@propensive
propensive / tmatch.scala
Created January 27, 2016 21:40
Type Matching in Scala
scala> :paste
// Entering paste mode (ctrl-D to finish)
val fn = tmatch(
when[Int] { i => i + 1 }
when[String] { s => s.toInt }
when[Symbol] { s => 3.1415926 }
when[Double] { d => "You entered a double" }
)
@propensive
propensive / xmldemo.scala
Last active February 20, 2018 11:54
Really Basic Rapture XML basic demo
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.xml._
import rapture.xml._
scala> import xmlBackends.stdlib._
import xmlBackends.stdlib._
@propensive
propensive / rapturejsonerrors.scala
Created August 6, 2015 08:08
Rapture JSON erorrs
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.json._
import rapture.json._
scala> import jsonBackends.jawn._
import jsonBackends.jawn._