Skip to content

Instantly share code, notes, and snippets.

@felipeblassioli
Created October 4, 2023 03:10
Show Gist options
  • Save felipeblassioli/2fbcda0ee8e788411dbefb6bdaad94b0 to your computer and use it in GitHub Desktop.
Save felipeblassioli/2fbcda0ee8e788411dbefb6bdaad94b0 to your computer and use it in GitHub Desktop.
Scala2 and Scala3 type projections test
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)
@felipeblassioli
Copy link
Author

felipeblassioli commented Oct 4, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment