Proposal: First class types (at compiletime)

Commander Zot no at no.no
Thu Jul 20 15:11:39 UTC 2023


On Thursday, 20 July 2023 at 14:46:49 UTC, H. S. Teoh wrote:
> On Thu, Jul 20, 2023 at 01:44:15PM +0000, Commander Zot via 
> Digitalmars-d wrote:
>> 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);
>> 
>> ```
> [...]
>
> What are you trying to accomplish that needs to do this?  If 
> you describe your use case, maybe we can better understand why 
> you can't just use a simple template:
>
> 	template Min(T, U) {
> 		static if (T.sizeof <= U.sizeof)
> 			alias Min = T;
> 		else
> 			alias Min = U;
> 	}
> 	alias V = Min!(int, short);
> 	static assert(is(V == short));
>
>
> T

yes, you could write it that way, but the same is true for 
value-based calculations.
CTFE allows us to write normal functions instead of templates for 
value logic at compiletime, we're just lacking first class type 
support to also use them for type logic compiletime.
currently we have runtime functions in std.algorithm and the same 
in std.meta for types for example. it would be nice if we could do
```
import std.algorithm;
alias TS = (int, short, byte).filter!(t=>t.sizeof>1).aliasseq;
```


and it wouldn't need too much changes i think (but i might be 
wrong there):
- make type parameters and return types implicitly convert to 
TypeInfo
- make typeinfo/typeid work properly in CTFE
- make an asignment to an alias convert a TypeInfo into the 
corresponding type at compiletime.
- add some special functions to TypeInfo, like comparison, 
sizeof, ...




More information about the Digitalmars-d mailing list