Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:24
Show Gist options
  • Select an option

  • Save dacr/d13d2da454dd1267ba9fbaaca85e6506 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/d13d2da454dd1267ba9fbaaca85e6506 to your computer and use it in GitHub Desktop.
scala3 feature examples - anonymous class inference / published by https://github.com/dacr/code-examples-manager #23c62324-a693-4395-97ed-e5040e976d71/810b16335fc5fda5063cdcb5c356a79936869f0d
// summary : scala3 feature examples - anonymous class inference
// keywords : scala3, tutorial, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : 23c62324-a693-4395-97ed-e5040e976d71
// created-on : 2021-04-21T13:59:22+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
//> using scala "3.4.2"
trait Message {
val text: String
}
def dumpMessage(message:Message) = println(message.text)
// ---------------------------------------------------------------
abstract class Person {
val name: String
}
def sayHello(person:Person) = println("Hello "+person.name)
// ---------------------------------------------------------------
trait MutableStuff {
var message: String=""
}
def someStuff(stuff:MutableStuff) = println(stuff.message)
// ---------------------------------------------------------------
@main def go():Unit = {
dumpMessage(new {val text="Bouh!"})
sayHello(new {val name="John"})
someStuff(new {message="hummm?"}) // When the field is a var with an already defined and so overrable value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment