p5h / p5summit-2019

Perl 5 Summit
0 stars 0 forks source link

Custom infix operators #10

Open leonerd opened 4 years ago

leonerd commented 4 years ago

perl 5.14 added the keyword hook, which lets XS modules implement new keywords. It's great. I use it lots. But it is limited to only prefix keywords - the word must appear first to introduce new syntax. While this is generally nice - for humans as well as perl - it does mean that XS modules can't provide even two of the most commonly-requested infix operators:

$obj isa ClassName  # true if blessed($obj) and $obj->isa("ClassName")

$x in @y  # true if any { $x eq $_ } @y

While it would be giving XS authors a lot of rope by which to hang themselves, a small extension to the keyword hook mechanism could allow such infix operators to be created.

leonerd commented 4 years ago

Counterpoint: This might be considered too dangerous for what is really just a minor spelling change from

isa $obj, ClassName
in $x, @y

so it might not be worth the risk exposed by it