-
-
Save yadavan88/a66b338294cd4633ae3fce486876c26c to your computer and use it in GitHub Desktop.
Sample code using Shapeless Tagged Types.
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._ | |
object TaggedTypes { | |
trait UserId | |
trait Name | |
val userId = tag[UserId][String]("yadu0123") | |
val name = tag[Name][String]("yadukrishnan") | |
def printInfo(userId: String @@ UserId, name: String @@ Name) = { | |
println(s"HEY, userId: ${formatString(userId)} and name:${formatString(name)}") | |
} | |
def formatString(str: String) = s"`${str}`" | |
def main(args: Array[String]): Unit = { | |
printInfo(userId, name) | |
// printInfo(name, userId) //compilation error | |
// printInfo("hey","heyhey") //compilation error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More detailed explanation provided in the blog: https://yadukrishnan.hashnode.dev/tagged-types-in-scala