Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/d13d2da454dd1267ba9fbaaca85e6506 to your computer and use it in GitHub Desktop.
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/d80c7ed07648aea40ef5995c164ae132588fc92a
// summary : scala3 feature examples - anonymous class inference
// keywords : scala3, tutorial, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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.1.1"
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