Skip to content

Create "Autobuild fields from enum type param" #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More practical example
  • Loading branch information
Geokureli authored Nov 6, 2024
commit 0a9969053897ea240a75f077cb94ecd1689f4215
77 changes: 50 additions & 27 deletions assets/content/cookbook/Macros/add-enum-values-as-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,66 @@ The macro is used via [@:autoBuild](https://haxe.org/manual/macro-auto-build.htm

```haxe
class Test {
static function main() {
final grades = new GradeSet();
grades.add(A);
grades.add(F);
grades.add(C);
trace(grades.A); // true
trace(grades.B); // false
trace(grades.C); // true
trace(grades.D); // false
trace(grades.F); // true
}
static function main() {
// poised dagger damage types
final dmg = new DamageSet([Poison, Piercing, Slashing]);
trace(dmg.Acid); // false
trace(dmg.Bludgeoning); // false
trace(dmg.Cold); // false
trace(dmg.Fire); // false
trace(dmg.Force); // false
trace(dmg.Lightning); // false
trace(dmg.Necrotic); // false
trace(dmg.Piercing); // true
trace(dmg.Poison); // true
trace(dmg.Psychic); // false
trace(dmg.Radiant); // false
trace(dmg.Slashing); // true
trace(dmg.Thunder); // false
}
}

class GradeSet extends EnumSet<Grade> {}

enum Grade { A; B; C; D; F; }
class DamageSet extends EnumSet<DamageType> {}

enum DamageType {
Acid;
Bludgeoning;
Cold;
Fire;
Force;
Lightning;
Necrotic;
Piercing;
Poison;
Psychic;
Radiant;
Slashing;
Thunder;
}

@:autoBuild(Macro.buildSet())
class EnumSet<E:EnumValue> {
final list = new Array<E>();
final list = new Array<E>();

public function new() {}
public function new(list:Array<E>) {
for (item in list)
add(item);
}

inline public function has(value:E):Bool {
return list.contains(value);
}
inline public function has(value:E):Bool {
return list.contains(value);
}

public function add(value:E) {
if (has(value) == false)
list.push(value);
}
public function add(value:E) {
if (has(value) == false)
list.push(value);
}

public function remove(value:E) {
list.remove(value);
}
public function remove(value:E) {
list.remove(value);
}
}
```
Notice that fields A, B, C, D, E and F were added to GradeSet
Notice that fields A, B, C, D, E and F were added to DamageType

> Author: [George Kurelic](https://github.com/geokureli)
Loading