Re: [PATCH for Dlang support 1/2] d: create the Parser.Context class
Akim Demaille <[email protected]>
| Newsgroups | gmane.comp.parsers.bison.patches |
|---|---|
| Message-ID | <[email protected]> |
Hi H.S., > Le 27 oct. 2020 à 17:55, H. S. Teoh <[email protected]> a écrit : > > On Tue, Oct 27, 2020 at 07:38:46AM +0100, Akim Demaille wrote: > [...] >>> + /** >>> + * Information needed to get the list of expected tokens and to forge >>> + * a syntax error diagnostic. >>> + */ >>> + public static final class Context >>> + { >>> + >>> + private YYStack yystack; >> >> Is this a copy of the stack? It does not look like a >> reference/pointer to the this. We must not copy it, it's big, and >> readonly here. It should be "const", but I don't know how that is >> spelled in D. > [...] > > Is YYStack a struct or class? Or an array? If it's an array or class, > it's a reference type by default. It's a struct. But I guess it's not so bad as it contains only an array: private final struct YYStack { private YYStackElement[] stack = []; ... so after all, it is not so big, I expect the inner array to be "copied" by reference. > W.r.t. const, there are several ways to spell it, depending on the > intended semantics. If it should be an immutable reference, then you > could write it as: > > private const(YYStack) yystack; > > In this case, the variable cannot be reassigned after initialization, > and if it's a reference type, it cannot be rebound. Also, any elements > you retrieve from it will be const (const in D is transitive). I believe this is what we need here. > If it's an array, you can optionally have a mutable slice of immutable > elements (i.e., a view that can change, but the underlying elements are > not changeable): > > private const(E)[] yystack; // where E is the element type of YYStack Now I see why const takes parens. Thanks a lot for your explanations!