Suppose I want to define a function whose argument must be a List of only Strings and List must have at least one member. I know how to require that the Head must be a List but how does one qualify List members within an argument? I really don't want to resort pattern matching inside the body of my definitions or trailing a conditional pattern outside my definitions.
This is what I have so far along with a few test cases...
fn[arg_List && AllTrue[arg, StringQ] && Length@arg > 0] :=
Block[{}, arg];
fn[{"a", "b", "c"}] (* should return {"a","b","c"} *)
fn[{1, "b", "c"}]
fn[{}]
Unfortunately it produces no errors which in this case would have been very helpful.
fn[arg_List /; (AllTrue[arg, StringQ] && Length@arg > 0)] := Module[{}, arg];
? btw, why Block there? Why not use normalModule
? $\endgroup$because they require more memory and are slower than blocks
I did not know this. I use Modules all the time, since Block scoping is a little confusing to me. But if they work for you, that should work then. $\endgroup$fn[arg_ /; VectorQ[arg, StringQ] && arg != {}] := ...
$\endgroup$