Skip to content

Instantly share code, notes, and snippets.

@leifwickland
Created February 6, 2016 17:55
Show Gist options
  • Save leifwickland/5a1ab6efa6f822019a3a to your computer and use it in GitHub Desktop.
Save leifwickland/5a1ab6efa6f822019a3a to your computer and use it in GitHub Desktop.
sealed trait Pokemon {
def name: String
def elemental: String
}
// I want a macro like this.
// (The type names may need to need to be in strings.)
// (I'm not entirely sure that quasiquote macros can do this magic.)
enum[Pokemon](
Bulbasaur -> ("フシギダネ", "Grass"),
Ivysaur -> ("フシギソウ", "Grass"),
Venusaur -> ("フシギバナ", "Grass"),
Charmeleon -> ("リザード", "Fire"),
Charizard -> ("リザードン", "Fire"),
Squirtle -> ("ゼニガメ", "Water"),
Charmander -> ("ヒトカゲ", "Fire")
)
// Generates the equivalent of:
abstract class PokemonBase(val name: String, val elemental: String) extends Pokemon
object Bulbasaur extends PokemonBase("フシギダネ", "Grass")
object Ivysaur extends PokemonBase("フシギソウ", "Grass")
object Venusaur extends PokemonBase("フシギバナ", "Grass")
object Charmeleon extends PokemonBase("リザード", "Fire")
object Charizard extends PokemonBase("リザードン", "Fire")
object Squirtle extends PokemonBase("ゼニガメ", "Water")
object Charmander extends PokemonBase("ヒトカゲ", "Fire")
@bwmcadams
Copy link

I like the idea but i'd make the macro name "ADT" instead of "enum" so as not to confuse / mislead users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment