English 中文(简体)
将 Str 转换为 Enums 列表列表
原标题:converting list of Str into list of Enums
  • 时间:2024-07-25 12:48:08
  •  标签:
  • enums
  • raku
I have an enum definition in a module AAA: enum RDProcDebug ; class Debug { method foo(RDProcDebug @ds) { for @ds { say .key ~ => ~ .value } } } In Raku programs, this works well, eg use AAA; my RDProcDebug $rp .= new; $r.foo(AstBlock, BlockType); But now I need to supply the names of the debug elements from the command line, which can only supply Str, eg $ RDOpts= AstBlock BlockType raku myprog # and in myprog if %*ENV:exists { $rp.debug( %*ENV ); # this does not work } So how do I convert a list of Str into a list of type enum RDProcDebug ?
最佳回答
Okay, so you want to pass those enum values from the command line, but there you only get strings. I get it. Here s what you can do: First, grab that RDOpts from your environment and split it into separate words. my @debug-options = %*ENV.split(/s+/); Now here s the neat part. Raku has a trick to look up enum values by their name. It looks a bit weird, but it works like this: my @debug-values = @debug-options.map({ RDProcDebug::{$_} }); What this does is check for each string in your list if there s a matching enum value. If all goes well, you ll end up with a list of actual RDProcDebug values. You can now just pass it to your debug method: $rp.debug(@debug-values); All together you would get something like this: if %*ENV { my @debug-options = %*ENV.split(/s+/); my @debug-values = @debug-options.map({ RDProcDebug::{$_} }); $rp.debug(@debug-values);} Oh yeah, one more thing: if someone types in a wrong name, you ll get an error. You might want to do something about that..
问题回答

暂无回答




相关问题
Finding the Highest Value in an Enumeration

I m writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value: pressedKeys = new BitArray(highestValueInEnum<Keys>());...

Conversion of Enum to Enumerable

To convert Enum to Enumerable ,I use public enum Flags { Trivial=1, Minor, Major, Critical } IEnumerable<int> n = Enumerable.Range((int)Flags.Trivial, (...

Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

Enum scoping issues

I try to keep things as local as possible, so I put enums at class scope, even if they are shared between two classes (I put it in the class that "goes better" with it.) This has worked out great, but ...

How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

C++ enum value initialization

I have an enum declared in my code as: enum REMOTE_CONN { REMOTE_CONN_DEFAULT = 0, REMOTE_CONN_EX_MAN = 10000, REMOTE_CONN_SD_ANNOUNCE, REMOTE_CONN_SD_IO, REMOTE_CONN_AL, ...

WCF: Enforce equal DataContracts on both sides

I m wondering if it is possible to have WCF make sure that the DataContracts on both sides of a connection are exactly the same (and throw an exception when trying to connect if they are not). For ...

热门标签