Austin Hastings writes:
> How can I conveniently pass an extra parameter to a historically binary
> operator?
I see a few possibilities. The first, call it like a function:
if infix:eqn("Dough", "Douglas", n => 4) {...}
Or, you could use the adverbial modifier C<where> (well, not officially
yet, but I hope so):
if "Dough" eqn "Douglas" where n=>4 {...}
But that has some problems, like putting the operator parameters really
far away from the operator, and forcing parentheses on a function call
on the right (lest the C<where> be associated with the call).
More below.
> At one point, the colon was going to do that, but Larry took it back:
>
> if ("Dough" eqn:4 "Douglas") # true
> { ...
>
> The options left seem to be:
>
> package String;
> sub strncmp($a, $b, $n) {...}
> package main;
>
> sub infix:immed &String::strncmp.assuming($n = 4);
You probably mean:
our &infix:immed ::= &String::strncmp.assuming($n => 4);
> if ("Dough" immed "Douglas") # ?
>
> or:
>
> class InfixBinary is Code {...}
> # How do I create a "type"? -- I want to define a return-type-spec
> # that just guarantees context behavior.
>
> sub eqn($max) returns InfixBinary {
> return &String::strncmp.assuming($n=$max);
> }
>
> if ("Dough" eqn(4) "Douglas") ...
>
> Frankly, I like the second one.
Yeah, I don't think it's a bad idea. I think returning an infix
operator is not the way to go about it. Perhaps optional (and named)
parameters on an infix operator could be taken from a parameter list:
sub infix:eqn ($a, $b, ?$n) {
substr($a, 0, $n) eq substr($b, 0, $n)
}
if "Dough" eqn(4) "Douglas" {...}
That's not terribly good at making different things look different.
It's pretty common to see parenthesized expressions on either side of an
operator.
Hmm, since we're requiring no whitespace between a variable and it's
subscript, this should be possible:
if "Dough" [eqn 4] "Douglas" {...}
If we're going that far...
sub foo($a, $b) {
print "$($a)y$b";
}
"Dough" [foo] "Douglas"; # DoughyDouglas
Not sure I like that too well.
Luke