but Skeleton
infix but
butThe rules for but have not been written yet. Everything below is extracted rather than authored: the official documentation supplies the classification, and the interpreters were asked directly about the rest.
What is established #
Classification #
but is parsed as an infix at the structural precedence level, non-associative.
That is level 14 of 27 on the ladder, counting from the tightest. Operators sharing a level bind equally; grouping among them is decided by associativity alone.
Shares its precedence level with #
does, cmp, coll, unicmp, leg, <=>, .., ..^, ^.., ^..^.
These all bind equally tightly, so an expression mixing them groups by associativity alone.
What the interpreter said #
Raku++ parses this spelling. Asked to compile:
my $a = 6; my $b = 3; sink($a but $b)
…it got through the parser. On its own that establishes very little: it says nothing about whether the construct executes, let alone whether the result is right. Where the behaviour table below exists, that is the stronger evidence — those expressions were actually run.
Covered upstream #
The official documentation has a section for this construct: docs.raku.org. Until the rules here are written, that is the better reference.
Observed behaviour #
Each expression below was executed by both interpreters when this page was built; the results are recorded, not written. The operands are chosen to cross the boundaries that catch people out: string against number, a list where a scalar was meant, a boolean, an undefined value, and an exact rational.
7 of 7 of these disagree between Raku++ and Rakudo — shown side by side below. A disagreement here is a defect report waiting to be written, not a documented rule.
| Expression | Raku++ | Rakudo |
|---|---|---|
1 but 2 | Int+{} | 1 | Int+{<anon|1>} | 1 |
"a" but "b" | Str+{} | a | Str+{<anon|1>} | a |
1 but "2" | Int+{} | 1 | Int+{<anon|1>} | 2 |
(1, 2) but (3, 4, 5) | List+{} | (1 2) | ERR Method 'Int' must be resolved by class List+{<anon|4>,<anon|5>,<anon|6>} because it exists in multiple roles (<anon|6>, <anon|5>, <anon|4>) |
True but False | Bool+{} | True | Bool+{<anon|1>} | False |
Nil but 1 | ERR Undefined routine 'but' | Nil+{<anon|1>} | Nil |
1/2 but 1/3 | Rat | 0.166667 | ERR Cannot resolve caller infix:<but>(Rat:D, Int:D, :value(Int)); none of these signatures matches: |