On Wed, 4 Dec 2019 at 08:45, ToddAndMargo via perl6-users
<perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:
Hi All,
I am cooking up something where I want top pass a value to a sub, but I
want to restrict what those values are.
For instance, things like
AbortRetryIgnore
CancelRetryContinue
Help
YesNo
Maybe
And so on and so forth.
If the wrong value is passed, I want the checker to crash
the sub.
What is the best way of going about this?
Many thanks,
-T
On 2019-12-04 01:28, Simon Proctor wrote:
So I'd approach this in one of two ways. Firstly there's the multi sub
with constants option :
multi selector( "AbortRetryIgnore" ) { ... }
multi selector( "CancelRetryContinue" ) { ... }
multi selector( "Help" ) { ... }
multi selector( "YesNo" ) { ... }
multi selector( "Maybe" ) { ... }
I'd do this if each option is pretty different in what happens.
If on the other hand the way the options are handled pretty similar I'd
go with and Enum :
enum Option <AbortRetryIgnore CancelRetryContinue Help YesNo Maybe>;
subset OptStr of Str where { Options::{$_}:exists};
multi sub selector( OptStr $opt ) { callwith( Options::{$opt} ) }
multi sub selector( Option $opt ) { ... }
This lets us all the sub with with the Option enum value eg :
selector(YesNo) or the string variant (useful on the command line) eg :
selector("YesNo")
I'd do this if there's a lot of shared functionality in the selector sub.
Hope that helps.
You can combine the two options of course :)
enum Option <AbortRetryIgnore CancelRetryContinue Help YesNo Maybe>;
subset OptStr of Str where { Options::{$_}:exists};
multi sub selector( OptStr $opt ) { callwith( Options::{$opt} ) }
multi selector( AbortRetryIgnore ) { ... }
multi selector( CancelRetryContinue) { ... }
multi selector( Help ) { ... }
multi selector( YesNo ) { ... }
multi selector( Maybe ) { ... }
Simon
Hi Simon,
You are way over my head. I am going to have to read it over several
times and do a lot of learning.
Thank you for the help!
-T