Created
October 4, 2023 03:10
-
-
Save felipeblassioli/2fbcda0ee8e788411dbefb6bdaad94b0 to your computer and use it in GitHub Desktop.
Scala2 and Scala3 type projections test
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
trait Logger { | |
protected def formatMessage(message: String): String | |
def log(message: String): Unit = { | |
println(formatMessage(message)) | |
} | |
// Nested case class inside the trait | |
case class LogEntry(timestamp: Long, content: String) | |
} | |
object Logger { | |
def apply(): Logger = new Logger { | |
override protected def formatMessage(message: String): String = | |
s"${System.currentTimeMillis()}: $message" | |
} | |
// Create a LogEntry instance using the nested case class in the trait | |
def createLogEntry(logger: Logger, content: String): logger.LogEntry = | |
logger.LogEntry(System.currentTimeMillis(), content) | |
// Process a LogEntry instance | |
def processLogEntry(entry: Logger#LogEntry): Unit = | |
println(s"Processing log entry: Timestamp = ${entry.timestamp}, Content = ${entry.content}") | |
} | |
// Usage | |
val logger = Logger() | |
val entry = Logger.createLogEntry(logger, "This is a log entry.") | |
Logger.processLogEntry(entry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dropped general type projection in Scala 3: https://docs.scala-lang.org/scala3/reference/dropped-features/type-projection.html
See: https://blog.rockthejvm.com/scala-3-type-projections/