Skip to content

Instantly share code, notes, and snippets.

Here's a systematic way to remove F-bounds from your code. It's inspired by the algebraic subtyping encoding from Stephen Dolan's PhD thesis:
If you have an F-bounded type T <: F[T] simply replace it with T. Then everywhere T is used, replace it with T & F[T].
I tested that strategy with the example from @tpolecat's blog on F-bounds.
Here's how it comes out:
trait Pet[A]:
this: A =>
def name: String
@odersky
odersky / adhocpoly.scala
Last active February 18, 2022 13:08
A Scala translation of James Ward's ContextReceivers example
// A literal Scala translation of James Ward's Context receivers example
// https://github.com/jamesward/oop-evolution/blob/context-receivers/gradleable/src/main/kotlin/adhocpoly/ContextReceivers.kt
// I had to rename `sum` to `summ` since `sum` already exists as a method on lists in Scala.
package adhocpoly
trait Summable[T]:
def tplus(t1: T, t2: T): T
object Summable:
def tplus[T](t1: T, t2: T)(using s: Summable[T]) = s.tplus(t1, t2)
@odersky
odersky / standalone-scala.md
Created February 20, 2021 10:47
Standalone Scala

Standalone Scala

Or: Simpler Scala Tooling

(Draft, 20-Feb-2020)

Today, almost all Scala tooling depends on a build tools. This has several undesirable consequences:

  • The beginning Scala programmer has to learn the language and a build tool.
  • There are several possible build tools (e.g. sbt, mill, Maven, gradle, bazel), which leads to confusion and fragmentation.
  • The historically most widely used build tool, sbt, has a reputation of being very complex, which contradicts our positioning of scala as a great tool for beginners.

Principled Meta Programming for Scala

This note outlines a principled way to meta-programming in Scala. It tries to combine the best ideas from LMS and Scala macros in a minimalistic design.

  • LMS: Types matter. Inputs, outputs and transformations should all be statically typed.

  • Macros: Quotations are ultimately more easy to deal with than implicit-based type-lifting

  • LMS: Some of the most interesting and powerful applications of meta-programming

object Phantom {
type Nothing <: Any
type Any
def assume[T <: Any]: T
class Function1[-T <: Any, +R <: Any] {
def apply(x: T): R
}
@odersky
odersky / A simpler way to returning the "current" type in Scala.
Last active April 5, 2024 13:34
A simpler way to returning the "current" type in Scala.
/** This is in reference to @tploecat's blog http://tpolecat.github.io/2015/04/29/f-bounds.html
* where he compares F-bounded polymorphism and type classes for implementing "MyType".
*
* Curiously, the in my mind obvious solution is missing: Use abstract types.
*
* A lot of this material, including an argument against F-bounded for the use-case
* is discussed in:
*
* Kim B. Bruce, Martin Odersky, Philip Wadler:
* A Statically Safe Alternative to Virtual Types. ECOOP 1998: 523-549
package scalax.collection
import scala.collection.mutable.ListBuffer
/** FoldTransformers and the views based on them are a Scala
* adaptation, and to some degree an extension, of Rich Hickey's
* transducers for Clojure. They show that the concepts can be
* implemented in a type-safe way, and that the implementation is
* quite beautiful.
*/
object FoldingViews {
@odersky
odersky / equality.scala
Last active October 6, 2018 18:56
Better equality for Scala
import annotation.unchecked.uncheckedVariance
/** The trait of things that can be compared safely */
trait Equals[-T] {
/** A witness of Equals' type parameter. Should only used for
* the constraint in EqlDecorator, hence, @uncheckedVariance should not be a problem.
*/
type EqualsDomain = T @uncheckedVariance