View TaggedTypeMistake.sc
This file contains 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
val userId: Long = 12 | |
val deviceUuid: Long = 1234 | |
val carSerialId: Long = 12345 | |
def getHashCode( | |
userId: Long, | |
deviceUuid: Long, | |
carSerialId: Long | |
): String = { | |
val userIdPlus: Long = userId + 1 |
View SolveProblemByUsingCaseClass.sc
This file contains 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
case class UserIdCaseClass(userId: Long) | |
case class DeviceUuidCaseClass(deviceUuid: Long) | |
case class CarSerialIdCaseClass(carSerialId: Long) | |
def getHashCodeCaseClass( | |
userIdCaseClass: UserIdCaseClass, | |
deviceUuidCaseClass: DeviceUuidCaseClass, | |
carSerialIdCaseClass: CarSerialIdCaseClass | |
): String = { | |
val userIdPlus: Long = userIdCaseClass.userId + 1 |
View TaggedTypeAnswer.sc
This file contains 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 shapeless.tag | |
import shapeless.tag.@@ | |
trait UserIdTag | |
trait DeviceUuidTag | |
trait CarSerialIdTag | |
type UserId = Long @@ UserIdTag | |
type DeviceUuid = Long @@ DeviceUuidTag | |
type CarSerialId = Long @@ CarSerialIdTag |
View taggedTypeOverrideFail.sc
This file contains 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 getId(userId: UserId): Long = userId + 1 | |
def getId(deviceUuid: DeviceUuid): Long = deviceUuid + 2 // Not Compiled because tagged type erased after compile | |
def getId(carSerialId: CarSerialId): Long = carSerialId + 3 // Not Compiled because tagged type erased after compile |
View TaggedTypeEither.sc
This file contains 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 getUserIdOrDeviceIdOrCarSerialId( | |
id: Either[UserId, Either[DeviceUuid, CarSerialId]] | |
): Long = id match { | |
case Left(userId) => userId + 1 | |
case Right(Left(deviceUuid)) => deviceUuid + 2 | |
case Right(Right(carSerialId)) => carSerialId + 3 | |
} | |
getUserIdOrDeviceIdOrCarSerialId(Left(taggedUserId)) | |
getUserIdOrDeviceIdOrCarSerialId(Right(Left(taggedDeviceUuid))) |
View TaggedTypeShapeless.sc
This file contains 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 shapeless.{Coproduct, CNil, :+:, Inl, Inr} | |
type Id = UserId :+: DeviceUuid :+: CarSerialId :+: CNil | |
def getId(id: Id): Long = id match { | |
case Inl(userId) => userId + 1 | |
case Inr(Inl(deviceUuid)) => deviceUuid + 2 | |
case Inr(Inr(Inl(carSerialId))) => carSerialId + 3 | |
case Inr(Inr(Inr(cNil))) => 0 | |
} |
View TaggedType.sc
This file contains 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
val userId: Long = 12 | |
val deviceUuid: Long = 1234 | |
val carSerialId: Long = 12345 | |
def getHashCode( | |
userId: Long, | |
deviceUuid: Long, | |
carSerialId: Long | |
): String = { | |
val userIdPlus: Long = userId + 1 |
View Instance.sc
This file contains 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 Printable { | |
def print: String | |
} | |
case class PrintableClass(value: String) extends Printable { | |
override def print: String = s"value: ${value.toString}" | |
} | |
case class ValueCaseClass(value: String) |
View ImplicitDef.sc
This file contains 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
implicit def convertValueCaseClassToPrintable(valueCaseClass: ValueCaseClass): PrintableClass = | |
PrintableClass(valueCaseClass.value) | |
val printableClass: PrintableClass = ValueCaseClass("Implicit Def") // by using convertValueCaseClassToPrintable | |
println(printableClass.print) |
View ImplicitClass.sc
This file contains 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
implicit class ConvertableValueCaseClass(valueCaseClass: ValueCaseClass) { | |
def toPrintable: PrintableClass = PrintableClass(valueCaseClass.value) | |
} | |
val printableClass2: PrintableClass = ValueCaseClass("Implicit class").toPrintable // by using implicit class | |
println(printableClass2.print) |