TypeFunction example creatiing a conversion matrix
jmh530
john.michael.hall at gmail.com
Thu Oct 1 11:35:10 UTC 2020
On Thursday, 1 October 2020 at 09:54:25 UTC, Stefan Koch wrote:
> [snip]
>
> I mean on the other hand.
> Maybe I should be motivated.
> Now I can do a direct comparison to alternative implementations.
> Without having to write the alternative myself.
I certainly think it's interesting and seems more straightforward
than Andrei's reification example, but more concrete results will
help your case. You might make a few examples of both simple and
complex use cases of this technique and compare (at least
compile-time and the size of object files) to the current D
compiler and Andrei's reification approach. You might have a case
if you can show quantitative improvements over the alternatives.
One thing that keeps coming to my mind when you are providing
examples is the zig language's comptime [1]. They have the
example of
fn max(comptime T: type, a: T, b: T) T {
return if (a > b) a else b;
}
fn gimmeTheBiggerFloat(a: f32, b: f32) f32 {
return max(f32, a, b);
}
fn gimmeTheBiggerInteger(a: u64, b: u64) u64 {
return max(u64, a, b);
}
and obviously this could be done in D with templates. But if type
functions were in D, then I would imagine you should also be able
to do it like:
T max(alias T, T a, T b) {
return (a > b) ? a : b;
}
float gimmeTheBiggerFloat(float a, float b) {
return max(float, a, b);
}
uint gimmeTheBiggerInteger(uint a, uint b) {
return max(uint, a, b);
}
[1]
https://ziglang.org/documentation/master/#Introducing-the-Compile-Time-Concept
More information about the Digitalmars-d
mailing list