develooper Front page | perl.perl6.users | Postings from August 2017

Re: User defined infix operators and whitespace

Thread Previous
From:
Brock Wilcox
Date:
August 10, 2017 11:28
Subject:
Re: User defined infix operators and whitespace
Message ID:
CAFkkL_hWGGZtve-ShYZg8MQtrb2Uv-cic6HBcDgADRFVso7V_Q@mail.gmail.com
I think there are a couple answers. The simple one is yes -- embrace the
whitespace. Maybe wrap them in ()', like (2 d 6).

Another line of ideas is wrapping things with some other operators. Maybe a
special quoting operator or a converter.

  [[2d6]] # double for dice!
  "2d6":dice # postfix. This used to work but maybe doesn't now
  dice[ 2d6 ] # circumfix

And then the third thing that comes to mind is adding a slang, though that
is, I believe, the least stable language-level approach. Dunno.

--Brock


On Thu, Aug 10, 2017 at 7:16 AM, Simon Proctor <simon.proctor@gmail.com>
wrote:

> So I had a crazy little idea. I've played the odd bit of roleplaying in my
> time and wanted to created a 'd' operator.
>
> Quite simple really.
>
> sub infix:<d> ( Int $num, Int $size ) { [+] (1..$size).roll($num) };
>
> sub prefix:<d> ( Int $size ) { 1 d $size }
>
> Gives us 3 d 6 to roll 3 six sided dice or a prefix d 10 for a single 10
> sided dice.
>
> Except... I'd really like to write 3d6 or d10 but the parser barfs.
>
> Am I going to just have to live with that? Or did I miss something
> obvious?
>
> Obviously it's possible to have operators that ignore whitespace (1+1
> works just fine) but is it possibly for user defined ones?
>
> Possibly more serious ones.
>
> Simon
>

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About