Skip to content

martin-kolinek/scala-deriving

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple macro which allows for easy creation of type class instances based on existing instances.

This is inspired by newtype ... deriving ... syntax from haskell.

Example:

trait IsCat[T] {
    def meow(cat:T):String
    def mate(cat1:T, cat2:T):T
}

class Cat(val name:String) {
}

//this imitates the newtype
case class CatInABox(cat:Cat) {
} 

implicit val catIsCat = new IsCat[Cat] {
    def meow(cat:Cat) = "meow"
    def mate(cat1:Cat, cat2:Cat) = new Cat(s"${cat1.name} junior")
}

//here we create a derived instance based on existing instance
implicit val catInABoxIsCat = deriving[CatInABox, IsCat].equiv(_.cat, CatInABox)

val cat = CatInABox(new Cat("cat"))

implicitly[IsCat[CatInABox]].meow(cat)    

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages