Skip to content

Instantly share code, notes, and snippets.

View retronym's full-sized avatar

Jason Zaugg retronym

  • Lightbend
  • Brisbane, Australia
  • Twitter @retronym
View GitHub Profile
@retronym
retronym / type-bounds.scala
Created December 16, 2009 11:17
Tour of Scala Type Bounds
View type-bounds.scala
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x
@retronym
retronym / generalised-type-constraints.scala
Created November 8, 2009 08:02
Demo of generalised type constraints in Scala 2.8
View generalised-type-constraints.scala
// scala 2.7 simple type constraint. This can only constrain a type parameter of this function.
// Below, in ListW.sumint28, we can't use this approach because we want to constrain T,
// a type param of the enclosing trait.
def sumint27A[T <: Int](l: List[T]) : Int = l.reduceLeft((a: Int, b: Int) => a + b)
trait IntLike[X] extends (X => Int)
object IntLike {
implicit val intIntLike: IntLike[Int] = new IntLike[Int] { def apply(x: Int) = identity(x) }
}
@retronym
retronym / backport-process.md
Created November 14, 2012 13:32
Backport process
View backport-process.md

Avoiding backporting

If possible, target commits at the 2.10.x branch rather than master. This can be done if the change is:

  • Binary compatible (for scala-library)
  • Source compatible (for unofficial compiler API used by SBT, IDE)
  • Well isolated, doesn't involve sweeping structural changes
  • Well tested. (but this must be true, otherwise it wouldn't be on master, right?!)

If such changes were targeted against master and now must be applied to 2.10.x, here's how to do it.

@retronym
retronym / log.txt
Created November 10, 2022 06:43
SBT bill of materials
View log.txt
tail local.sbt project/local.sbt
==> local.sbt <==
TaskKey[Unit]("printBillOfMaterials") := (Def.taskDyn {
val proj = thisProject.value
val filter = ScopeFilter(inProjects(proj.aggregate: _*))
Def.task {
(updateSbtClassifiers.value :: update.all(filter).value.toList).flatMap(_.configurations).flatMap(_.modules).flatMap(_.artifacts).flatMap(_._1.url).distinct.foreach(println)
()
}
}).value
@retronym
retronym / proxy.scala
Created November 21, 2010 16:23
proxy.scala
View proxy.scala
package mapmap
trait ECtx
trait KCtx
trait Var[C, T] {
def get(c: C): T
}
@retronym
retronym / Kayo.bsstrategy
Last active October 26, 2022 06:26
Beardie Config for Stan Sports and Kayo (https://github.com/Bonapara/beardie)
View Kayo.bsstrategy
BSStrategy = {
version: 1,
displayName: "Kayo",
accepts: {
method: "predicateOnTab",
format: "%K LIKE[c] '*kayosports.com.au/*/*'",
args: ["URL"]
},
toggle: function () { console.log("toggle"); document.querySelector('[aria-label="Toggle play/pause"]').click(); },
pause: function () { console.log("pause") },
View 0.test.scala
class covariant extends scala.annotation.StaticAnnotation with scala.annotation.TypeConstraint
object SetFactory {
def empty[K]: Set[K @covariant] = Set.empty[K @covariant]
}
class Client {
val x = if (true) Set("") else SetFactory.empty
}
@retronym
retronym / power-mode.scala
Created July 19, 2010 18:04
Using Power Mode in the Scala REPL to find the parameter names of a method
View power-mode.scala
scala> :power
** Power User mode enabled - BEEP BOOP **
** scala.tools.nsc._ has been imported **
** New vals! Try repl, global, power **
** New cmds! :help to discover them **
** New defs! Type power.<tab> to reveal **
scala> val s = repl.stringToCompilerType("scala.Some")
s: repl.compiler.Type = object Some
@retronym
retronym / indylambda.md
Last active February 5, 2022 10:47
indylambda: Putting invokedynamic to work for Scala
View indylambda.md

indylambda: Putting invokedynamic to work for Scala

Java 8 introduced lambdas to the Java language. While the design choices differ in many regards from Scala's functions, the underlying mechanics used to represent Java lambdas is flexible enough to be used as a target for the Scala compiler.

Lambdas in Java

Java does not have canonical heirarchy of generic function types (ala scala.FunctionN), but instead allows a lambda to be used as a shorthand for an anonymous implementation of an Functional Interface

Here's an example of creating a predicate that closes over one value:

@retronym
retronym / IteratorLinter.scala
Created January 16, 2022 23:42
iterator escape
View IteratorLinter.scala
package demo.compat
import scala.collection.{GenIterableLike, mutable}
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
import scala.tools.nsc.{Global, Phase, Settings}
object IteratorLinter {
def main(args: Array[String]): Unit = {
println("go!")
val g = new Global(new Settings) {