Skip to content

Instantly share code, notes, and snippets.

View joshlemer's full-sized avatar
🏠
Working from home

Josh joshlemer

🏠
Working from home
  • Winnipeg, Canada
View GitHub Profile

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

Here's how you can add a type variable to not maintain two parallel-but-similar case classes:

case class UserEntityF[F[_]](id: Option[Long] = None, username: F[String], password: F[String])

type Id[A] = A

// You can also just use Option if you don't care
// for the domain-specific type
sealed trait Updatable[+A]
case class Update[A](a :A) extends Updatable[A]
@milessabin
milessabin / singleton-only.scala
Created June 6, 2016 11:04
Scala type which can only be extended by an object, not by a non-abstract type ...
scala> class Foo { self: Singleton => }
defined class Foo
scala> class Bar extends Foo
<console>:12: error: illegal inheritance;
self-type Bar does not conform to Foo's selftype Foo with Singleton
class Bar extends Foo
^
scala> object Bar extends Foo
@acdenhartog
acdenhartog / 0-CurryingUpdate
Last active March 14, 2016 09:41
Why the update method should have been curried in the Scala language specification:
This text describes much of what is wrong with the update sugar in Scala.
Each part should be able to drop directly into a repl/worksheet and compile.
See 2-TL;DR:.scala for motivation.
@non
non / answer.md
Last active January 9, 2024 22:06
answer @nuttycom

What is the appeal of dynamically-typed languages?

Kris Nuttycombe asks:

I genuinely wish I understood the appeal of unityped languages better. Can someone who really knows both well-typed and unityped explain?

I think the terms well-typed and unityped are a bit of question-begging here (you might as well say good-typed versus bad-typed), so instead I will say statically-typed and dynamically-typed.

I'm going to approach this article using Scala to stand-in for static typing and Python for dynamic typing. I feel like I am credibly proficient both languages: I don't currently write a lot of Python, but I still have affection for the language, and have probably written hundreds of thousands of lines of Python code over the years.

@viktorklang
viktorklang / Future-retry.scala
Last active July 23, 2023 23:48
Asynchronous retry for Future in Scala
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
@mbostock
mbostock / .block
Last active February 14, 2024 20:43 — forked from mbostock/.block
Cluster Dendrogram
license: gpl-3.0
height: 2000
border: no
redirect: https://beta.observablehq.com/@mbostock/d3-cluster-dendrogram