Skip to content

Instantly share code, notes, and snippets.

@Jasper-M
Jasper-M / implicitly1.scala
Created February 28, 2018 18:50
Implicits that are only implicitly available
scala> object Module {
| type Foo[T] = Internal.Foo[T]
| object Foo {
| def pubMethod = "Hi!"
| }
|
| private[Module] object Internal {
| trait Foo[T]
| object Foo {
| implicit def FooString = new Foo[String] {
@Jasper-M
Jasper-M / HasCompanion.scala
Last active November 2, 2022 02:18
Typeclass that provides evidence that a type has a companion object, and access to the object.
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
trait HasCompanion[A] {
type Type
def companion: Type
}
object HasCompanion {
type Aux[A,C] = HasCompanion[A] { type Type = C }
def apply[A](implicit hc: HasCompanion[A]): hc.type = hc
@Jasper-M
Jasper-M / TypeAliasImplicitScope.scala
Last active July 26, 2017 14:20
Giving an implicit scope to an abstract type member that erases to Object
sealed trait FooImplicitScope
object FooImplicitScope {
implicit def Foo2String(f: Module.Foo): String = f.toString
implicit def Foo2Int(f: Module.Foo): Int = f.##
}
object Module {
private[Module] type A
type Foo <: A with FooImplicitScope
}
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> class Bar { type T; def bar = weakTypeTag[T] }
defined class Bar
scala> object Barr extends Bar { type T = Int }
defined object Barr
scala> val tag = Barr.bar
@Jasper-M
Jasper-M / Main_fails1.scala
Last active December 9, 2016 15:48
dotty implicit function types
object ImplicitFunShowTest {
import Showing._
def showList[T](list: List[T]) = list.map(t => show(t): String)
// no implicit argument of type Showing.Show[T] found for parameter x$0 of method apply in trait ImplicitFunction1
def main(args: Array[String]): Unit = {
println( showList(List("a","b","c")) )
println( showList(List(1,2,3)) )
}
}