Hi Moritz

>> a:b is the same as a..b  (this is the easy part)
>>
>> a:b:c is a range from 'a' to 'b' by steps of 'c'. For example, 2:15:3
>> == 2,5,8,11,14
>
> That can be done by giving the new infix:<:> operator list associativity
> (niecza already supports this)

Can you explain or give me a link that explains how to do that? I can
figure out a little bit on my own:

multi sub infix:<:>( Int $a, Int $b ) {
     #  This is the case 'a:b'
}

multi sub infix:<:>( Range $a, Int $b ) {
     #  Is this correct?
     #  I'm thinking that a:b:c could be interpreted as (a:b):c so it
is "Range" : "Int".
     # Am I on the right track?
}


>> :b is the same as 0..b
>
> create a new prefix operator... except that that prefix : is already
> taken for constructing pairs

Ok. I hadn't realized that you could define the same symbol as prefix,
infix and postfix... So.. ignoring the fact that : conflicts with
existing syntax, I could do this:

multi sub infix:<:>( Int $a, Int $b )  { ... }
multi sub infix:<:>( Range $a, Int $b ) { ... }
multi sub prefix:<:>( Int $a ) { ... }
multi sub postfix:<:>( Int $b ) { ... }

Right?

>> : is the same as 0..Inf
>
> create a term:<:>.

Ok.


> There are hooks for all of that, but it requires you to come up with a
> new syntax that doesn't collide heads-on with lots of existing grammar
> rules.

Thanks. I'll see if I think of something that feels natural but
doesn't collide with anything. One option is to just use the existing
.. operator:

multi sub prefix:<..>( Int $b ) { 0..$b }
multi sub postfix:<..>( Int $a ) { $a..Inf }

Only problem is that something like "2..10..3" might be confusing.

Hmm... I must have made a mistake here somewhere... After defining
these functions, the traditional range '2..10' doesn't work anymore. I
suppose that my new functions have a higher precedence than the
traditional .. operator. I tried to fix this, but I failed:

multi sub prefix:<..>( Int $b ) is looser(&infix:<cmp>) { 0..$b }

So, I'm not doing this right.

Cheers.
Daniel.
-- 
I'm not overweight, I'm undertall.

Reply via email to