TypeFunction example creatiing a conversion matrix

Adam D. Ruppe destructionator at gmail.com
Thu Oct 1 21:34:50 UTC 2020


On Thursday, 1 October 2020 at 21:04:29 UTC, Walter Bright wrote:
> Does the same thing, works today:

I think it is actually a mistake to focus too much on types.

The "type function" concept might be useful if it provides a way 
to work with compiler tuples in general. Then it might be able to 
do things that are basically impossible right now (well at least 
not without a dose of string mixin) and do it efficiently.

I've been playing with a variety of other implementations and 
they keep ending up slow. A tuple foreach unrolls into a bunch of 
code. Perhaps this can be further optimized with a few tweaks:

1) make a function an any temporaries coming from it as CTFE only 
so it isn't codegen'd, optimized in any way, or emitted to the 
object file.

2) maybe borrow part of Stefan's implementation to avoid 
unrolling certain tuple looks so it runs faster. We already have 
`foreach(alias t; T)` so maybe it can just be optimized.

But then we still have the difficulty of translating the result 
tuple back into something we can use. The only facilities the 
language provides right now are some kind of recursive template 
and/or some kind of string mixin.

These get expensive again (especially for long and/or varied 
argument lists) and may hit other arbitrary limitations like 
runtime/compile time barriers. And it is a bit obnoxious because 
the compiler already knows all this stuff, but instead of saying 
"reuse that please" we have to say "turn that into a string, then 
turn the string into a template, then turn the template into a 
tuple".

So some facility to turn a ctfe array back into a tuple - the 
dereifiy step basically - with compiler assistance I think will 
be practical.


And you know, I'm still a little skeptical of the type functions, 
but I do think they have potential when we start looking at them 
as tuple manipulators more than as template replacements. Tuples 
are this weird thing that are half in the language and half 
not... and bringing them more in would actually be pretty nice.

Would it solve Variant's needs? Well ... I think that's separate, 
we actually can do that in today's D. You can make a list of 
conversion targets in opAssign, make one in get, and compare them 
at runtime. Maybe I'll implement later but she's fussing again 
ttyl


More information about the Digitalmars-d mailing list