semi-final switch?

Steven Schveighoffer schveiguy at gmail.com
Fri Jun 18 12:18:56 UTC 2021


On 6/17/21 5:54 PM, H. S. Teoh wrote:
> On Thu, Jun 17, 2021 at 05:41:28PM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote:
> [.[..]
>> Oh, and to throw a monkey wrench in here, the value is a string, not
>> an integer. So I can't use std.conv.to to verify the enum is valid
>> (plus, then I'm running a switch twice).
>>
>> Any ideas on better ways to handle this?
> [...]
> 
> Why not just:
> 
> 	try {
> 		MyEnum value = input.to!MyEnum;
> 	} catch (Exception e) {
> 		stderr.writeln("Invalid input");
> 	}
> 

Yeah, that was a possibility I listed. Of course `to` is going to do a 
switch, which means then after I validate the value is valid, I then 
have to do a final switch to process it. I'd prefer to do it both in one 
switch.

-Steve


More information about the Digitalmars-d-learn mailing list