Proposal: First class types (at compiletime)

Commander Zot no at no.no
Thu Jul 20 13:44:15 UTC 2023


wouldn't it be possible to have something like first class types 
(at compile time) to replace tamplates for type logic with 
regular functions executed at CTFE?

the idea is to make this work
```
type_t min(type_t a, type_t b){ return a.sizeof<=b.sizeof?a:b; }
alias r = min(short, int);

```
basically an equivalent to
```
int min(int a, int b){return a<=b?a:b;}
enum r = min(2, 4);
```

here's a working example how it's kind of possible in D atm, but 
lacks the syntax sugar:


```
struct Foo {
     struct Bar {
     }
}

struct type_t {
private:
     size_t _size;
     string _ts;
public:
     size_t sizeof_() {
         return _size;
     }
}

auto t(T)() {
     import std.traits : fullyQualifiedName;
     enum tis = fullyQualifiedName!T;
     return type_t(T.sizeof, tis);
}

template r(type_t xt) {
     mixin("alias r = " ~ xt._ts ~";");
}

// use normal functions for type logic instead of template magic 
(basically a CTFE equivalant for type logic)
auto min_type(type_t a, type_t b) {
     return a.sizeof_ <= b.sizeof_ ? a : b;
}

void main() {
     enum XT = t!(Foo.Bar);
     alias T = r!(XT);
     pragma(msg, T);

     alias ST = r!(min_type(t!short, t!int));
     pragma(msg, ST);
}
```




More information about the Digitalmars-d mailing list