ADT abstract database logic that uses macros as a tag

I recently stumbled upon a rather amazing piece of code from Travis Brown @ Sealed iteration in Scala? . It turns ADT into a Scala sealed trait + case class/object into something closer to true enumerations, allowing the enumeration of ADT constructors (in Haskell terms). Thus, I removed 2 of the 3 warning warnings that the Travis code generated and ended with http://pastebin.com/L1gYGJWh , which I then used as follows:

 sealed trait KeywordType case object Crime extends KeywordType case object Gambling extends KeywordType case object Porn extends KeywordType object KeywordType { /** Maps eg "Crime" to Crime */ def withName(x: String): Option[KeywordType] = adt.enumerate[KeywordType].find(_.toString === x) } 

However, I very quickly had to use the same bit withName logic, so I tried to write the following basic ADT tag:

 trait ADT[T] { def all: Set[T] = enumerate[T] def withName(name: String): Option[T] = all.find(_.toString === name) } 

However, this does not compile if:

 Can only enumerate values of a sealed trait or class 

Although I understand why this error occurs, I have no idea how to talk about how a system like β€œdefers” a check while it inherits from ADT .

Is this possible with the current macro system in Scala 2.11.2 or do I need to find a (partial) workaround?

+5
source share

Source: https://habr.com/ru/post/1202551/


All Articles