Closed GallaFrancesco closed 3 years ago
I have trouble understanding this code (like why is this a delegate) and why this change fixes the problem. Good job figuring this out! I'd be interested if you can share more clarity, but I'll merge this anyway because it clearly works better this way. Thanks!
I have trouble understanding this code (like why is this a delegate) and why this change fixes the problem.
Perhaps, because this :
string failMsg(string delegate(Position, string, string, const ParseTree) formatFailMsg = defaultFormatFailMsg,
string successMsg = "Sucess") const @property
{
failMsg expects a delegate to format fail messages.
Why this change fixed? I don't know, but kudos to @GallaFrancesco for finding and fixing it.
failMsg
expects a delegate because delegates can be passed down as function arguments, see https://tour.dlang.org/tour/en/basics/delegates and https://dlang.org/spec/function.html#closures
I'm no expert, but the reason why using auto
does not work at compile time could be that failMsg
accepts a delegate which should be initialized at compile time. immutable
global declaration with explicit initialization are treated as constants (which allows for CTFE) while auto
global declarations are not. See the following example:
auto deg = delegate(string str) { return "deg: "~str;};
shared static this() {
pragma(msg, deg("ciao"));
}
immutable deg = delegate(string str) { return "deg: "~str;};
shared static this() {
pragma(msg, deg("ciao"));
}
That helps. Thanks both of you.
Trying to generate a grammar whose PEG syntax is wrong at compile time with pegged
v0.4.5
and DMDv2.095.0
orv2.096.1
fails with:<
into<--
(or any symbol not recognized by pegged which is not anidentifier
) in one of theexample/
grammars, such as thesimple_arithmetic
above. Then run./ci.sh
defaultFormatFailMsg
declaration fromauto
toimmutable
. The result is a properly formatted fail msg: