Proposal: First class types (at compiletime)
Commander Zot
no at no.no
Fri Jul 21 13:11:04 UTC 2023
On Friday, 21 July 2023 at 12:46:12 UTC, Stefan Koch wrote:
> On Friday, 21 July 2023 at 12:42:06 UTC, Commander Zot wrote:
>
>>
>> thx. but as i understand it, this is quite different then what
>> i proposed, right?
>> my proposal doesn't implement 'type functions' as a special
>> kind of function, it just uses TypeInfo as a parameter and
>> return type.
>>
>> so my idea would be to allow any type to implicitly convert to
>> it's TypeInfo when a type is used in a expression.
>> and any TypeInfo to be converted back to it's type when
>> assigned to an alias.
>> TypeInfo[] should be converted back to an AliasSeq of those
>> types.
>
> In order to convert TypeInfo[] into real types you need to
> build special code into the compiler.
> The information contained in TypeInfo[] is not by itself enough
> to create a type.
> All of the transformation on types or TypeInfo need to be
> threaded through the compiler.
yes, you need a TypeInfo like type_t in my example.
for reference here's how my example currently works with
TypeInfo[]:
```d
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 ~";");
}
template r(type_t[] xts) {
import std.algorithm;
import std.array;
import std.meta;
mixin("alias r = AliasSeq!(" ~
xts.map!(t=>t._ts).array.join(",") ~");");
}
// use normal functions for type logic instead of template magic
(basically a CTFE equivalant for type logic)
auto both(type_t a, type_t b) {
return [a,b];
}
void main() {
enum XT = t!(Foo.Bar);
alias T = r!(XT);
pragma(msg, T);
alias ST = r!(both(t!short, t!int));
pragma(msg, ST);
}
```
this is already working in current D. it's just annoying that you
have to write t! and r! everywhere.
the idea is to lower
```
alias T = foo(short, int);
```
into some sort of
```
alias T = type_from_typeid(foo(typeid(short), typeid(int)));
```
More information about the Digitalmars-d
mailing list