3 ms·
I know of at least one other use of operator overloading which is quite pleasant to use. In Lua's PEG engine, Lpeg, operator overloading is used to express comb
by samatman 6y ago
I know of at least one other use of operator overloading which is quite pleasant to use. In Lua's PEG engine, Lpeg, operator overloading is used to express combinators, so `P"a" * P"b"` matches "ab" and `P"a" + P"b"` tries to match "a", then tries to match "b".
It's a little bit of a kludge! But a clever one, which uses the precedence of the existing operators to match the expected precedence of concatenation and the `/` ordered choice operator from PEG grammars.
In general I'm suspicious of arguments that boil down to "people might use this language feature badly". I'm not sure I want the full ability to add my own infix and postcircumfix operators, the way e.g. Raku allows, but there's no denying that being able to write `if foo ∈ test_set` is expressive and cool.
Maybe expressive and cool isn't a terminal value for language design, maybe `if test_set.element(foo)` is good enough. I kinda like it though.
- b2gills 6y agoThe Raku idea is that an operator only does one thing. That way you can tell at a glance what the operator is doing. If you want to do something else, create a new operator. (One of the design mottos was “Similar things should look similar, and different things should look different”.) In languages which only let you overload existing operators; those operators often get used for a bunch of different things. I mean, how many languages use `+` for both addition and string concatenation? … and maybe also set union?
- dragonwriter 6y ago> The Raku idea is that an operator only does one thing. Its kind of interesting that Raku’s own docs think of '=' being used for both item assignment and lost assignment as an exception to this, because it illustrates how narrowly Raku defines “one thing”. (As does the use of different postcircumfix operators for positional and associative access, which most languages call indexing, and use one construct for, which may not even technically be an operator.) But its not just freedom to create new operators that allows this; for it to work Raku has a huge number of built-in basic operators, plus the hyper- and meta-operators.
- b2gills 6y agoActually `=` sort-of only does one thing. It asks the left argument what it wants to do with the values. my @a = 1,2,3; is almost the same as my @a; @a.VAR.STORE((1,2,3)); --- As for separate positional and associative access: Raku has separate positional and named arguments to functions. An argument list is actually a singular thing called a Capture. Even if it usually pretends to not be. my \capture = \( 'a', b => 10 ); # Capture litteral say capture[0]; # a say capture{'b'}; # 10 sub foo ( |capture ) { say capture[0]; # a say capture{'b'}; # 10 } foo |capture; # give it the above capture litteral # foo 'a', b => 10 sub bar ( $a, :$b ){ say $a; # a (positional) say $b; # 10 (named) } bar |capture; # give it the above capture litteral # bar 'a', b => 10 To be able to easily extract out those parts, it helps if there is a different way to get at each. The same thing also applies to extracting information out of regexes 'a10' ~~ / (.) # $0 $<b> = (..) # $<b> /; say $0; # a say $<b>; # 10 say $/[0]; # a (positional index into $/) say $/{'b'}; # 10 (named index into $/)