Skip to content

Instantly share code, notes, and snippets.

View mbloms's full-sized avatar

Mikael Blomstrand mbloms

View GitHub Profile
@mbloms
mbloms / obnoxious-indentaion-bikeshedding-sip.md
Last active December 10, 2020 18:36
My take on brace-less syntax for Scala 3

An alternative Optional Braces syntax for Scala 3

This is my view of what a dream scenario for the new syntax in Scala 3 would look like, and why I think it's better than the current syntax implemented in Dotty.

A diff of the EBNF syntax can be found here: https://github.com/lampepfl/dotty/commit/9638ebfeec002554897836b2c97f70e371f26cdd

The main goal in this alternative is to prioritize ease of reading over ease of writing. Code is generally written once, and then read (and edited) over and over again.

My only two remaining grievances with the current syntax is this:

  • Giving new meaning to : is confusing. It's one of Scala most important symbols that already has two meanings depending on whether it's a context bound or type declaration.
package vector
import scala.reflect.ClassTag
import collection.{immutable,mutable}
import mutable.Cloneable
/** Const Interface for a Wrapped Array
* An instance of this class is either a MutableArray or ImmutableArray
*/
sealed abstract class ConstArray[+A] protected (arr: Array[A])

Pre-SIP: Transparent members for Opaque types

I think opaque type aliases is one of Scala 3's most interesting features. Many use cases for it has already been discussed. One oppertunity that I think would be quite useful, while at the same time being a very good fit with the rest of the language, is the ability to expose members of the underlying type as members on the opaque type.

For example: The current imlementation of IArray uses extension methods to export methods from Array:

The precence of the asInstanceOf as a method on Any is something which has annoyed me since I first understood how it works. As a Scala beginner coming from Java googling how to do type casts in Scala what you will get is "in Scala it's asInstanceOf". So most people explaining Scala to a Java developer would say that x.asInstanceOf[C] is equivalent to (C) x.

Looking at the rules for asInstanceOf on primitive types also indicates that this could have been the purpose in the beginning.

Now, every time there is a proposal for a new interesting feature in the type system, at some point inevitably someone will say "But what about x.asInstanceOf?". To which the answer is always "asInstanceOf is a universal escape hatch, which comes with no guarantees."

Any interesting research concerning Scala's type system must be done in a subset of Scala without asInstanceOf since of course otherwise nothing can actually be proved.

It's clear to me that people perceive asInstanceOf as one of two things:

  • T
package huset
sealed class CanMutate {
type C
}
final class ShallowUnique[T >: Null <: AnyRef] private[huset] (private var instance: T) {
final def borrow(body: ((x: T) => (CanMutate {type C = x.type } => T))): Unit = {
val x = instance
given perm as CanMutate {type C = x.type}
@mbloms
mbloms / CapturedTest.Xprint:typer.scala
Last active September 17, 2020 17:02
Improved version: Captured types are aggregated into a Union Type
//result of CapturedTest.scala after typer:
package <empty> {
import scala.spores._
final lazy module val CapturedTest: CapturedTest$ = new CapturedTest$()
final module class CapturedTest$() extends Object() {
this: CapturedTest.type =>
def main(args: Array[String]): Unit =
{
val hejdu: String = "hejdu"
val s: spores.NullarySpore[Unit]{Captured = String | Int} =
package witness
import scala.language.implicitConversions
import scala.reflect._
import scala.collection.immutable._
/**
* Base trait for a Captured instance of type T
* TODO: Naming
* @tparam T
object Bank {
def placeFillOrKillBuyOrder(price: Double, vol: Int, stock: StockID, buyer: UserID): Unit {
val (askPrice,askVol) = OrderDepth.getLowestAsk(stock)
if (price >= askPrice && vol <= askVol) {
/* The Bank updates the user's account to reflect
the trade which it was able to fill at once. */
Account.decreaseBalance(UserID, price * vol)
Account.increaseStock(UserID, stock, vol)
Account.notify("Stock bought successfully")
@mbloms
mbloms / bitcoin
Created September 26, 2019 01:14
35Rfd6XRdC8djSjXKpUGhaAKAMp8jykfaM
@mbloms
mbloms / keybase.md
Last active December 8, 2019 16:38

Keybase proof

I hereby claim:

  • I am mbloms on github.
  • I am mbloms (https://keybase.io/mbloms) on keybase.
  • I have a public key ASB1SZakMPgiARxfJHQRiocrMR_WLekinxDkiifO1YayJAo

To claim this, I am signing this object: