smuj wrote:
> TSa wrote:
>>
>> HaloO,
>>
>> David Green wrote:
>>>
>>> For certain discrete ordered types, like Int, both ways work out the
>>> same, and since Ints are the most common and most obvious use for Ranges,
>>> it's easy to overlook the confusion.  The case with strings is a good
>>> example: it really doesn't make sense that a value not produced by a range
>>> nevertheless lies between its endpoints.  Why not have a separate Interval
>>> type?
>>
>> I see no problem when a Range matches for values which are not produced
>> by a RangeIterator. I expect 2.5 ~~ 1..5 to be true even though 2.5 is
>> not in 1,2,3,4,5.
>
> I suspect that the double meaning of Ranges is going to confuse some people
> and bite others. If things stay as they are, I hope that the use of :by will
> be flagged as a syntax error if used in literal Range smart matching. Of
> course, that doesn't help the unsuspecting when variables are being used,
> ala 2.5 ~~ $myrange.

Another possibility is that literal Range smartmatching works as is in
the absence of :by (that is, with a Range), but becomes a set
membership test in its presence (i.e., with a RangeIterator).  Or not;
see below.

> (For the record, 2.5 ~~ '!'..5 is also true on my system, although I don't
> know why! I certainly wouldn't expect it though :)

One explanation would be that it's comparing the String "2.5" to the
String-terminated Range "!".."5".  Since "2" falls between "!" and
"5", so does "2.5".

>> The same applies for 'aaa' ~~ 'aa'..'az'. I find this
>> quite natural.
>
> Not sure if you're saying that's something you'd like or if you think that
> that's something already there. It doesn't match for me using recent(ish)
> Rakudo. Of course, that could just be me! :)
>
> I'd personally prefer it if Ranges just did lists, including when smart
> matching, but had an interval method or such like for explicit matching
> against the endpoints, e.g.
>
> 2.5 ~~ interval(1..5)   # or
> 2.5 ~~ $myrange.interval

I don't like the Huffman encoding: "does $x come after $a and before
$b?" is a common test, and so should be short.  I'd rather require you
to force it into list context if your goal is to test for set
membership.  In fact, that might be a clean way of handling its dual
nature: in item context, it behaves as a Range object; in list
context, it behaves as the RangeIterator.  So:

    2.5 ~~ 1..5 # true: equivalent to "2.5 ~~ 1 <= $_ <= 5".
    2.5 ~~ @(1..5) # false: equivalent to "2.5 ~~ (1, 2, 3, 4, 5)".

Incidently, this first example is why I think that Range is intimately
related to the various order-related operators, and in particular
before and after: its most common use outside of generating sequential
lists is to provide a shorthand for "$min before $_ before $max" and
similar range-testing expressions.

-- 
Jonathan "Dataweaver" Lang

Reply via email to