digitalmars.D.learn - Use members of a Named Enum without using Enum name?
- QuizzicalFella (20/20) Aug 15 2015 I have a named enum that I'd like to keep named, that I'd like to
- Adam D. Ruppe (12/20) Aug 15 2015 Two options come to mind:
- QuizzicalFella (7/14) Aug 15 2015 ...if I wanted to write a mixin that iterated over all the
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (21/37) Aug 15 2015 Fundamentally, __traits(allMembers) and .stringof but the following
I have a named enum that I'd like to keep named, that I'd like to use as a type, but every time I use a member I'd rather not write out the enum name. I have a situation like the following: enum PolygonT : byte { TRIANGLE, RECTANGLE, STAR } void someFunc(PolygonT shape) { //some stuff } I'd like to be able to call someFunc(TRIANGLE) rather than someFunc(PolygonT.TRIANGLE). Being clear but not being too long is my aim. Writing the type makes it too verbose and I have lazy fingers... I don't mind if adding another enum with members of the same name like this: enum CelestialBodiesT : byte { MOON, SUN, BLACK_HOLE, STAR } ...would force me to call someFunc(PolygonT.STAR) by the compiler, but for the case where there's no ambiguity, I don't want to write out the type of the enum... I know I can add in an 'alias this' for classes and structs, but I think I only get one and if it's an enum I'm using all over the place I don't want to 'alias this' it everywhere... Is there a way I can do this while retaining type safety? (can I be lazy and protected at the same time?)
Aug 15 2015
On Saturday, 15 August 2015 at 15:37:42 UTC, QuizzicalFella wrote:I'd like to be able to call someFunc(TRIANGLE) rather than someFunc(PolygonT.TRIANGLE).Two options come to mind: alias TRIANGLE = PolygonT.TRIANGLE; // etc Or at the usage site: with(PolygonT) { someFunc(TRIANGLE); }I don't mind if adding another enum with members of the same name like this: enum CelestialBodiesT : byte { MOON, SUN, BLACK_HOLE, STAR } ...would force me to call someFunc(PolygonT.STAR) by the compiler, but for the case where there's no ambiguity, I don't want to write out the type of the enum...That'll work as long as the two enums are defined in separate modules, then only import the one you need to use at the time. Otherwise, they will conflict and it will force you to write it out long form again.
Aug 15 2015
On Saturday, 15 August 2015 at 15:53:23 UTC, Adam D. Ruppe wrote:On Saturday, 15 August 2015 at 15:37:42 UTC, QuizzicalFella wrote:...if I wanted to write a mixin that iterated over all the elements in an enum, how would I get a member to print its name without the type? And how do I get the type to print itself? foreach(member; enum) char[] output ~= "alias "~member.name~"="~enum.name~"."~member.name~";"I'd like to be able to call someFunc(TRIANGLE) rather than someFunc(PolygonT.TRIANGLE).Two options come to mind: alias TRIANGLE = PolygonT.TRIANGLE; // etc
Aug 15 2015
On 08/15/2015 09:22 AM, QuizzicalFella wrote:On Saturday, 15 August 2015 at 15:53:23 UTC, Adam D. Ruppe wrote:Fundamentally, __traits(allMembers) and .stringof but the following enumMembers present them as a range: import std.stdio; enum PolygonT : byte { TRIANGLE, RECTANGLE, STAR } auto enumMembers(E)() { import std.conv : to; import std.algorithm : map; return [ __traits(allMembers, E) ].map!(a => a.to!string); } void info(E)() { writefln("The members of %s: %-(%s, %)", E.stringof, enumMembers!PolygonT); } void main() { info!PolygonT(); } AliOn Saturday, 15 August 2015 at 15:37:42 UTC, QuizzicalFella wrote:...if I wanted to write a mixin that iterated over all the elements in an enum, how would I get a member to print its name without the type? And how do I get the type to print itself? foreach(member; enum) char[] output ~= "alias "~member.name~"="~enum.name~"."~member.name~";"I'd like to be able to call someFunc(TRIANGLE) rather than someFunc(PolygonT.TRIANGLE).Two options come to mind: alias TRIANGLE = PolygonT.TRIANGLE; // etc
Aug 15 2015