This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package polyglotprogramming.messaging.v1 | |
import polyglotprogramming.messaging.* | |
object Processor: | |
def apply(message: IncomingMessage): OutgoingMessage = message match | |
case Start(details) => StartHandler(message.asInstanceOf[Start]) | |
case DoWork1(details) => DoWork1Handler(message.asInstanceOf[DoWork1]) | |
case DoWork2(details) => DoWork2Handler(message.asInstanceOf[DoWork2]) | |
case Stop(details) => StopHandler(message.asInstanceOf[Stop]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package polyglotprogramming.messaging.v1 | |
import polyglotprogramming.messaging.* | |
trait MessageHandler[IM <: IncomingMessage]: | |
def apply(message: IM): OutgoingMessage | |
case object StartHandler extends MessageHandler[Start]: | |
def apply(message: Start): OutgoingMessage = | |
println(s"Received message: $message") | |
Succeeded(s"Successfully processed $message") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package polyglotprogramming.messaging | |
/** | |
* Use a parent trait and case class subtypes. I don't use "sealed" for the | |
* IncomingMessage types, because in v4 I'll show how nicely we can add | |
* new message types with minimal, yet robust code changes. | |
* | |
* @param details string as a simple way of returning information. | |
*/ | |
trait IncomingMessage(details: String) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.quoted.* | |
@main def InlinePerf(numberOfTrials: Int*) = | |
class Thing(var count: Int, var label: String) | |
def trial(n: Int) = | |
val startEnabled = System.nanoTime() | |
val thing1 = new Thing(0, "label") | |
for i <- 1 to n | |
do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from "Programming Scala, Third Edition" (http://programming-scala.com) | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/meta/Invariant.scala | |
import scala.quoted.* | |
object invariantMinInline: | |
val ignore = false | |
inline def apply[T]( // Use of "minimal" inlining. Does it still work? | |
predicate: => Boolean, // Now we need by-name parameters! | |
message: => String = "")( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from "Programming Scala, Third Edition" (http://programming-scala.com) | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/meta/Invariant.scala | |
import scala.quoted.* | |
object invariantDisabled: | |
inline val ignore = true | |
inline def apply[T]( | |
inline predicate: Boolean, | |
inline message: String = "")( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from "Programming Scala, Third Edition" (http://programming-scala.com) | |
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/meta/Invariant.scala | |
import scala.quoted.* | |
object invariantEnabled: | |
inline val ignore = false | |
inline def apply[T]( | |
inline predicate: Boolean, | |
inline message: String = "")( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from Programming Scala, Third Edition code examples. | |
// https://github.com/deanwampler/programming-scala-book-code-examples | |
// src/script/scala/progscala3/typesystem/matchtypes/DepTypedMethods.scala | |
type ElemR[X] = X match // "R" for "recursive" | |
case String => Char | |
case Array[t] => ElemR[t] | |
case Iterable[t] => ElemR[t] | |
case Option[t] => ElemR[t] | |
case AnyVal => X |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> val seq = Seq(1, 2, 3.14, 5.5F, "one", "four", true, (6, 7)) | |
| | |
val seq: Seq[Matchable] = List(1, 2, 3.14, 5.5, one, four, true, (6,7)) | |
scala> val result = seq.map { | |
| case 1 => "int 1" // <2> | |
| case i: Int => s"other int: $i" | |
| case d: (Double | Float) => s"a double or float: $d" // <3> | |
| case "one" => "string one" // <4> | |
| case s: String => s"other string: $s" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def examine1[T](seq: Seq[T]): Seq[String] = seq map { | |
case i: Int => s"Int: $i" | |
case other => s"Other: $other" | |
} | |
def examine2[T <: Matchable](seq: Seq[T]): Seq[String] = seq map { | |
case i: Int => s"Int: $i" | |
case other => s"Other: $other" | |
} |