Shopping for template languages
cc
cc at nevernet.com
Thu Aug 8 15:31:11 UTC 2024
On Wednesday, 7 August 2024 at 19:44:57 UTC, monkyyy wrote:
> why am I tolerating d?
I really, really like D's templating and metaprogramming. You
get the engine bits and wiring in under the hood, and then the
front-facing interface is clean and expressive, with a huge
amount of the work being done compile-time. It's just way more
fun to work in than anything else.
```d
@TABLE("characters")
struct Character {
enum Class {
NULL, FIGHTER, KNIGHT, ARCHER, MAGE, HEALER
}
enum Elements {
NONE = 0,
EARTH = 1<<0,
AIR = 1<<1,
FIRE = 1<<2,
WATER = 1<<3,
}
@NULLABLE @PRIMARY int id; // auto_increment
string name;
@ENUMSTRING @COLUMN("class") Class cClass;
int rarity;
@ENUMSTRING Elements elements;
SysTime mdate;
}
void main() {
auto db = DBH("chars.db"); // SQLite
auto table = db.createTable!Character;
table.insert(Character(name: "Bob", cClass:
Character.Class.KNIGHT, mdate: Clock.currTime));
table.insert(Character(name: "Joe", elements:
Character.Elements.EARTH | Character.Elements.FIRE, mdate:
Clock.currTime));
auto sth = db.query("SELECT * FROM characters;");
foreach (chara; sth.as!Character) {
writeln(chara.toStringer);
auto u = chara.updater;
u.mdate = Clock.currTime;
db.update(u);
}
}
```
```
Character(id:1, name:"Bob", cClass:KNIGHT, rarity:0,
elements:NONE, mdate:2024-Aug-09 00:18:05.457)
Character(id:2, name:"Joe", cClass:NULL, rarity:0,
elements:EARTH|FIRE, mdate:2024-Aug-09 00:18:05.465)
```
```
sqlite> SELECT * FROM characters;
┌────┬──────┬────────┬────────┬────────────┬───────────────┐
│ id │ name │ class │ rarity │ elements │ mdate │
├────┼──────┼────────┼────────┼────────────┼───────────────┤
│ 1 │ Bob │ KNIGHT │ 0 │ NONE │ 1723130285473 │
│ 2 │ Joe │ NULL │ 0 │ EARTH,FIRE │ 1723130285482 │
└────┴──────┴────────┴────────┴────────────┴───────────────┘
```
I'm aching to improve this some more if we could get a fix to
allow this:
```d
void foo(Nullable!int d) {}
foo(3); // compilation error
```
More information about the Digitalmars-d
mailing list