Q: index(Hello, , 999)

2005-03-16 Thread Markus Laire
What should index(Hello, , 999) return in perl6?
In perl5 that returns 5, but IMHO -1 would be right result.
--
Markus Laire
Jam. 1:5-6


Re: Q: index(Hello, , 999)

2005-03-16 Thread Aaron Sherman
On Wed, 2005-03-16 at 10:31, Markus Laire wrote:
 What should index(Hello, , 999) return in perl6?
 
 In perl5 that returns 5, but IMHO -1 would be right result.

Urk... exception?

Seriously, if you give index a position that is off the end of the
string, your options should be:

a. Extend the string
b. Fail silently
c. Throw an exception

a. is kind of non-Perlish. b. is Perl 5ish, but I'm not convinced it's
the right solution in all cases. In this case, specifically, I think it
should be c. (perhaps with some use nonfatal that causes library
functions to fail more gracefully).

That said, the current Perl 5 behavior is very broken. You should never
return POS unless it's a negative number flagging failure.

-- 
Aaron Sherman [EMAIL PROTECTED]
Senior Systems Engineer and Toolsmith
It's the sound of a satellite saying, 'get me down!' -Shriekback




Re: Q: index(Hello, , 999)

2005-03-16 Thread Larry Wall
On Wed, Mar 16, 2005 at 05:31:17PM +0200, Markus Laire wrote:
: What should index(Hello, , 999) return in perl6?
: 
: In perl5 that returns 5, but IMHO -1 would be right result.

Well, neither of those is the right result, since index is probably
not going to be returning integers in Perl 6, but string positions
which might have different integer values depending on whether you
look at them as bytes, codepoints, graphemes, etc.  And the string
position value will be true for valid string positions and false/undef
otherwise.

When you call index, it's going to assume that 999 is to be converted
to a string position under the current Unicode pragma, which turns
out to be undefined on the string in question, so your call above
probably either warns or throws an exception because you tried to
use an undefined value to do something defined.

Larry


Re: Q: index(Hello, , 999)

2005-03-16 Thread Thomas Sandlaß
Markus Laire wrote:
What should index(Hello, , 999) return in perl6?
Since the first thing that needs definition is how does
the empty string match, it could be e.g. any(0..Hello.elems).
As described in A12 string positions are much more these days
than simple ints. There is a class StrPosition or Pos[Str]
or Pos of Str or some such. So one signature could be:
multi sub index( Str   $searched,
 Str   $substr,
 Pos[Str] ?$pos where { 0 = $_  $searched.elems }
   )
  returns  Undef[Pos[Str]]
 ^ Pos[Str] where { 0 = $_  $searched.elems }
{
  ...
}

In perl5 that returns 5, but IMHO -1 would be right result.
This is because of the usage of . Otherwise it's -1.
Regards,
--
TSa (Thomas Sandlaß)



Re: Q: index(Hello, , 999)

2005-03-16 Thread Larry Wall
On Wed, Mar 16, 2005 at 06:44:47PM +0100, Thomas Sandlaß wrote:
: Markus Laire wrote:
: What should index(Hello, , 999) return in perl6?
: 
: Since the first thing that needs definition is how does
: the empty string match, it could be e.g. any(0..Hello.elems).

If the position of the first attempt is specified, the null string
should match right there.  It's an infinite loop if they don't add 1
next time (implying our position type needs to be able to add and
subtract in whatever the current units are).

One nit, .elems is not defined on strings.  .chars works under whatever
the current Unicode regime is (default graphemes).

: As described in A12 string positions are much more these days
: than simple ints. There is a class StrPosition or Pos[Str]
: or Pos of Str or some such. So one signature could be:
: 
: multi sub index( Str   $searched,
:  Str   $substr,
:  Pos[Str] ?$pos where { 0 = $_  $searched.elems }
:)
:   returns  Undef[Pos[Str]]
:  ^ Pos[Str] where { 0 = $_  $searched.elems }
: {
:   ...
: }
: 
: 
: 
: In perl5 that returns 5, but IMHO -1 would be right result.
: 
: This is because of the usage of . Otherwise it's -1.

Well, that, and Perl 5 is being kind in rounding 999 down to 5 on
the assumption that the programmer was specifically using 999 to mean
end of string.  It's kind of stupid to assume that for index, but
rindex is another matter.

Larry