8000 GitHub - rcorre/enumap: A D type that provides a lightweight mapping of enum members to values.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

rcorre/enumap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Enumap

An Enumap is a glorified wrapper around a static array. Are you sold yet? No? Alright, take a look at this:

enum Attribute {
 strength, dexterity, constitution, wisdom, intellect, charisma
}

Now you want to map each Attribute to an int. You could use D's built-in associative arrays:

int[Attribute] attributes;
attributes[Attribute.strength] = 10;

However, I think you'll like Enumap better:

Enumap!(Attribute, int) attributes;
attributes[Attribute.strength] = 10;

Still not impressed? Well, you might prefer an Enumap if:

You like syntactic sugar:

// Boring!
if (hero.attributes[Attribute.wisdom] < 5) hero.drink(unidentifiedPotion);

// Fun! And Concise!
if (hero.attributes.wisdom < 5) hero.drink(unidentifiedPotion);

You like ranges:

// roll for stats!
attributes = generate!(() => uniform!"[]"(1, 20)).take(6);

You like default values:

int[Attribute] aa;
Enumap!(Attribute, int) em;

aa[Attribute.strength]; // Range violation!
em.strength;            // 0

You like array-wise operations:

// note the convenient constructor function:
auto bonus = enumap(Attribute.charisma, 2, Attribute.wisdom, 1);

// level up! adds 2 to charisma and 1 to wisdom.
hero.attributes += bonus;

You dislike garbage day:

      void donFancyHat(ref int[Attribute] aa) { aa[Attribute.charisma] += 1; }
@nogc void donFancyHat(ref Enumap!(Attribute, int) map) { map.charisma += 1; }

Check the docs for the full feature set.

Enumap comes in dub package form.

About

A D type that p 5B2E rovides a lightweight mapping of enum members to values.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages

0