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) |
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 |
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 | |
} |
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))) |
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 |
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 |
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 |
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 |
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 val string: String = "Hello" | |
implicit val boolean: Boolean = true | |
implicit val int: Int = 0 | |
def getImplicitT[T](implicit t: T): T = t | |
getImplicitT[Int] | |
getImplicitT[String] | |
getImplicitT[Boolean] |
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 val optionInt: Option[Int] = Some(1) | |
implicit val optionBoolean: Option[Boolean] = Some(true) | |
def getImplicitOptionInt(implicit oInt: Option[Int]): Int = oInt.get | |
def getImplicitOptionBoolean(implicit oBoolean: Option[Boolean]): Boolean = oBoolean.get | |
getImplicitOptionInt | |
getImplicitOptionBoolean | |
def getImplicitlyOptionA[A: Option]: A = implicitly[Option[A]].get |