"Error: `TypeInfo` cannot be used with -betterC" on a CTFE function
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Tue Apr 9 12:45:55 UTC 2024
On 09/04/2024 12:48 PM, Liam McGillivray wrote:
> On Tuesday, 9 April 2024 at 00:02:02 UTC, Richard (Rikki) Andrew
> Cattermole wrote:
>> ```d
>> enum Value = (a, b) {
>> return a + b;
>> }(1, 2);
>> ```
>>
>> This alone should be a CTFE only function.
>>
>> But if we want template parameters, we'd need to wrap it with the
>> template.
>>
>> ```d
>> template Value(int a, int b) {
>> enum Value = () {
>> return a + b;
>> }();
>> }
>>
>> int value = Value!(1, 2);
>> ```
>>
>> Does that help?
>
> I had to reread this a few times to get a sense of what this is. I might
> have just got it. This is effectively a CTFE function for generating a
> constant based on the sum of two numbers, right? Doing `int value =
> Value!(1, 2);` would set `value` to 3, right?
Yes.
> I suppose this was a good new thing to learn, though I'm still quite far
> from being able to construct a function from another function using a
> template.
>
> I suppose that if I wanted it to make a function from another function,
> I may be able to do it in a template using some `static foreach` to make
> arrays of function parameters, and then combine them together without
> the use of strings, instead using placeholders (aliases or whatever
> they'd be called) and maybe the `tupleof` function. Am I headed in the
> right direction (if you can understand my weak attempt to describe the
> direction I'm thinking of going in)?
``tupleof`` isn't a function, its a property to get a "tuple" a sequence
of fields for a struct/class.
However most likely you'd have to resort to string mixins if you're
messing about with parameters like I think? you are asking for.
I'm not entirely sure what you're wanting there.
More information about the Digitalmars-d-learn
mailing list