Skip to content

Instantly share code, notes, and snippets.

@bbarker
bbarker / Example2b.scala
Created December 12, 2021 04:19
To see what is going wrong in outsFromInsAny, we can look at a simpler problem:
val outer1: Outer[Any] = Outer.apply(in1)
// ❌ Found: (Playground.in1 : Playground.Inner[String])
// Required: Playground.Inner[Any]
@bbarker
bbarker / Example2a.scala
Created December 12, 2021 03:42
List of nested invariants
case class Inner[A](a: A)
case class Outer[A](a: Inner[A])
val in1 = Inner("foo")
val in2 = Inner(3)
// Doesn't work as we'd hoped
val outsFromInsAny: List[Outer[Any]] = List(in1, in2).map(Outer.apply)
// ❌ Found: Playground.Inner[?1.CAP]
// Required: Playground.Inner[Any]
@bbarker
bbarker / Example1b.scala
Created December 12, 2021 03:39
Errors that occur when using "polymorphic" functions
// These don't work, but why?
val u1 = funPoly(someStuffEx)
// ❌ Found: (Playground.someStuffEx : List[Playground.SomeClass[?]])
// Required: List[Playground.SomeClass[A]]
// where: A is a type variable
val u2 = funAny(someStuffEx)
// ❌ Found: (Playground.someStuffEx : List[Playground.SomeClass[?]])
// Required: List[Playground.SomeClass[Any]]
// This works fine
@bbarker
bbarker / Example1a.scala
Created December 12, 2021 03:34
Declaring various "polymorphic" functions and associated data
case class SomeClass[A](a: A)
val someStuffAny: List[SomeClass[Any]] = List(SomeClass("foo"), SomeClass(1))
val someStuffEx: List[SomeClass[?]] = List(SomeClass("foo"), SomeClass(1))
def funPoly[A](ss: List[SomeClass[ A ]]): Unit = ()
def funAny(ss: List[SomeClass[Any]]): Unit = ()
def funEx(ss: List[SomeClass[ ? ]]): Unit = ()
@bbarker
bbarker / wildcards02.scala
Created December 12, 2021 03:30
Constraining wildcard types in a List
trait At
trait Bt extends At
trait Ct extends Bt
trait Dt extends Ct
trait Et extends Dt
val bcd: List[? <: Bt] = List(new Bt {}, new Ct {}, new Dt {})
val acd: List[? <: Bt] = List(new At {}, new Ct {}, new Dt {})
// ❌ Found: Object with Playground.At {...}
// Required: Playground.Bt
@bbarker
bbarker / wildcards01.scala
Created December 12, 2021 03:28
List of Any and List of ?
val anythings1: List[Any] = List("foo", 3.14, new java.lang.Object)
val anythings2: List[?] = List("foo", 3.14, new java.lang.Object)
@bbarker
bbarker / DiffxAssertions.scala
Created December 10, 2021 22:06
Pure assertion (matchesTo)
import com.softwaremill.diffx.{ConsoleColorConfig, Diff}
import zio.test.Assertion.Render.*
import zio.test.*
trait DiffxAssertions {
val matchesToAssertionName = "matchesTo"
/**
* Just like isTrue from zio-test, but with a different name parameter
@bbarker
bbarker / DiffxAssertionsM.scala
Created December 10, 2021 22:05
Effectful assertion (matchesToM)
import com.softwaremill.diffx.{ConsoleColorConfig, Diff}
import zio.UIO
import zio.test.AssertionM.Render.*
import zio.test.*
trait DiffxAssertionsM {
val matchesToAssertionNameM = "matchesTo"
/**
@bbarker
bbarker / build.sbt
Created December 10, 2021 22:03
zio-diffx coordinates
"io.github.bbarker" %% "zio-diffx" % "0.0.5" % Test
@bbarker
bbarker / build.sbt
Created December 10, 2021 22:02
jitpack resolver
resolvers ++= Seq(
"jitpack.io" at "https://jitpack.io/",
),