digitalmars.D.learn - Using a string generated at compile-time in a nogc function
- Mithun Hunsur (10/10) Apr 30 2016 Hi all,
- Nicholas Wilson (3/13) May 01 2016 Have you tried using a static (as in inside the function) static
- Mithun Hunsur (4/22) May 01 2016 I considered it briefly, but opted against it because it adds
- Anonymouse (9/20) May 01 2016 That does seem buggy but I don't know enough to say for certain.
- Mithun Hunsur (7/31) May 01 2016 Yup - that works. How odd! Along the same lines is
- Basile B (28/62) May 01 2016 Another solution:
Hi all, I'm working on removing the string mixins from my code, but have run into an issue: http://dpaste.dzfl.pl/ecd7eb53947e As far as I can tell, this should work; the enum should force compile-time execution (which it does, as evidenced by the pragma). I've worked around this by employing a string mixin, but it's not as clean: http://dpaste.dzfl.pl/021c4a849b32 Any insight would be appreciated :)
Apr 30 2016
On Sunday, 1 May 2016 at 05:28:36 UTC, Mithun Hunsur wrote:Hi all, I'm working on removing the string mixins from my code, but have run into an issue: http://dpaste.dzfl.pl/ecd7eb53947e As far as I can tell, this should work; the enum should force compile-time execution (which it does, as evidenced by the pragma). I've worked around this by employing a string mixin, but it's not as clean: http://dpaste.dzfl.pl/021c4a849b32 Any insight would be appreciated :)Have you tried using a static (as in inside the function) static array of strings?
May 01 2016
On Sunday, 1 May 2016 at 08:14:43 UTC, Nicholas Wilson wrote:On Sunday, 1 May 2016 at 05:28:36 UTC, Mithun Hunsur wrote:I considered it briefly, but opted against it because it adds unnecessary overhead (both time and space) - while minor, I'm not going to concede that quickly :PHi all, I'm working on removing the string mixins from my code, but have run into an issue: http://dpaste.dzfl.pl/ecd7eb53947e As far as I can tell, this should work; the enum should force compile-time execution (which it does, as evidenced by the pragma). I've worked around this by employing a string mixin, but it's not as clean: http://dpaste.dzfl.pl/021c4a849b32 Any insight would be appreciated :)Have you tried using a static (as in inside the function) static array of strings?
May 01 2016
On Sunday, 1 May 2016 at 05:28:36 UTC, Mithun Hunsur wrote:Hi all, I'm working on removing the string mixins from my code, but have run into an issue: http://dpaste.dzfl.pl/ecd7eb53947e As far as I can tell, this should work; the enum should force compile-time execution (which it does, as evidenced by the pragma). [...]That does seem buggy but I don't know enough to say for certain. I'd suggest filing a bug report anyway; the worst thing that can happen is that it gets closed. Unreported bugs can only be fixed by accident. Tacking an .idup after .toLower seems to make it work, at least on dpaste (http://dpaste.dzfl.pl/8abed3d3ec6c). I would have thought both toLower and idup returned a normal string, but unsure.enum loweredName = member.to!string.toLower.idup; pragma(msg, loweredName); if (member == test) return loweredName;
May 01 2016
On Sunday, 1 May 2016 at 10:37:23 UTC, Anonymouse wrote:On Sunday, 1 May 2016 at 05:28:36 UTC, Mithun Hunsur wrote:Yup - that works. How odd! Along the same lines is enum loweredName = "" ~ member.to!string.toLower; which also works without issues. This makes me think that the result of `member.to!string.toLower` isn't being treated as a string, despite being typed as one - and by using `idup` or concatenating to it, we essentially retype it as a string.Hi all, I'm working on removing the string mixins from my code, but have run into an issue: http://dpaste.dzfl.pl/ecd7eb53947e As far as I can tell, this should work; the enum should force compile-time execution (which it does, as evidenced by the pragma). [...]That does seem buggy but I don't know enough to say for certain. I'd suggest filing a bug report anyway; the worst thing that can happen is that it gets closed. Unreported bugs can only be fixed by accident. Tacking an .idup after .toLower seems to make it work, at least on dpaste (http://dpaste.dzfl.pl/8abed3d3ec6c). I would have thought both toLower and idup returned a normal string, but unsure.enum loweredName = member.to!string.toLower.idup; pragma(msg, loweredName); if (member == test) return loweredName;
May 01 2016
On Sunday, 1 May 2016 at 13:22:27 UTC, Mithun Hunsur wrote:On Sunday, 1 May 2016 at 10:37:23 UTC, Anonymouse wrote:Another solution: ---- import std.traits; import std.conv; import std.uni; import std.stdio; enum Test { A, B, C } auto enumToString(Test test) nogc nothrow { property string function() result = {return "";}; foreach (member; EnumMembers!Test) { if (member == test) { result = {return member.to!string.toLower;}; break; } } return result; } void main() { enumToString(Test.A)().writeln; } ---- returns a delegate, although the call is quite unfriendly, despite of the property.On Sunday, 1 May 2016 at 05:28:36 UTC, Mithun Hunsur wrote:Yup - that works. How odd! Along the same lines is enum loweredName = "" ~ member.to!string.toLower; which also works without issues. This makes me think that the result of `member.to!string.toLower` isn't being treated as a string, despite being typed as one - and by using `idup` or concatenating to it, we essentially retype it as a string.Hi all, I'm working on removing the string mixins from my code, but have run into an issue: http://dpaste.dzfl.pl/ecd7eb53947e As far as I can tell, this should work; the enum should force compile-time execution (which it does, as evidenced by the pragma). [...]That does seem buggy but I don't know enough to say for certain. I'd suggest filing a bug report anyway; the worst thing that can happen is that it gets closed. Unreported bugs can only be fixed by accident. Tacking an .idup after .toLower seems to make it work, at least on dpaste (http://dpaste.dzfl.pl/8abed3d3ec6c). I would have thought both toLower and idup returned a normal string, but unsure.enum loweredName = member.to!string.toLower.idup; pragma(msg, loweredName); if (member == test) return loweredName;
May 01 2016