strange CFTE issue

Boris-Barboris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 9 07:36:00 PDT 2017


On Wednesday, 15 March 2017 at 17:27:35 UTC, ag0aep6g wrote:
>> Phobos has it: std.meta.aliasSeqOf "converts an input range 
>> [...] to an
>> alias sequence." [1]
>
> Woops, forgot to give the URL for that "[1]". Here it is:
>
> http://dlang.org/phobos/std_meta.html#aliasSeqOf

Hello, I have a similar problem. For the life of me I can't make 
CTFE work while manipulating collections. Source:

import std.meta;
import std.traits;

template isFunctionField(OwnerType, string field_name)
{
	enum isFunctionField =
		isFunction(mixin(OwnerType.stringof ~ "." ~ field_name));
}

string[] sfilter(T)(string[] fields)
{
	string[] result;
	foreach (f; fields)
	{
		enum isfunc = isFunctionField!(T, f);
// variable f cannot be read at compile time.
// Even when I change isFunctionField from template to function 
that returns
// bool and takes "string field_name".
		if (isfunc)
			result ~= f;
	}
	return result;
}

string[] TypeFields(T)() pure
{
	enum field_names = [__traits(allMembers, T)];
	pragma(msg, typeof(field_names)); // prints string[]
	pragma(msg, field_names);         // prints correct member names
	enum filtered = sfilter!(T)(field_names);
	return filtered;
}

//then I call it by:
enum fields = TypeFields!(SomeStruct)();

   I can get string array of class members allright. Whenever I 
try to do any logic with it, I fail. I get that "enum field_names 
= " is not lvalue. How should I pass it to sfilter? When I change 
foreach loop into for loop, I can't index "string[] fields" array 
since "variable fields cannot be read at compile time".
   Should I pass field_names string array as some other type, 
AliasSeq or something?

   The thing is, on some level inside TypeFields, I will need to 
mutate array, I can't stick to tuples. I tried declaring array 
parameters immutable, but CTFE forbids casting from mutable into 
immutable. I'm kinda out of ideas.


More information about the Digitalmars-d-learn mailing list