5 ms·
Was: my $len = length($string); Now: my $len = $string.chars; My intuitive interpretation of that syntax would be "given a string, return a sequence co
by Someone 11y ago
Was: my $len = length($string);
Now: my $len = $string.chars;
My intuitive interpretation of that syntax would be "given a string, return a sequence containing its characters, in order".
Or is that overlading on return type, and do you get the behavior I would find intuitive if one assigns to an array?
Even if that is true, I find that ugly. I would prefer more explicit code, for example
my $len = $string.chars.#;
Similarly:
Was: $#array+1 or scalar(@array)
Now: @array.elems
(All IMHO)
- rihegher 11y agothis is a typo error actually: Now: my $len = $string.length; http://doc.perl6.org/type/Str#method_length http://doc.perl6.org/type/Str#method_length EDIT: I don't see any contact section on their page to let them know
- ksherlock 11y agomethod length Perl 6 does not have a length function. See chars or elems.
- nemo 11y agolength is gone. See: http://design.perl6.org/S29.html#Obsolete_Functions http://design.perl6.org/S29.html#Obsolete_Functions "length() This word is banned in Perl 6. You must specify units. In practice, this probably means you want Str.chars(), although it could be Str.bytes(), or even something else. See S32-setting-library/String for details."
- rcthompson 11y agoThat's an interesting design decision, and I suppose it solves the Python problem of "I want to operate on any iterable, except for strings which are technically iterables but I want to treat them as atomic elements." (Incidentally, does Python 3 have a solution to this problem?)
- rihegher 11y agoSorry about that, I've jumped a bit too fast on that one
- squiguy7 11y agoI agree with you. I think a chars functions isn't the best name in the sense that it returns the length of the string instead of a list of its individual characters. It makes me wonder if this will be the case for other methods in the language.
- xorcist 11y agoEvery Perl expression has a context, and context is as central to Perl as types are to some other languages. It's first chapter material in any Perl book. It's not "overloading on return type" strictly speaking, but that's what the syntax looks like. An array (of characters in your example) in a scalar context is the length of the array. I don't know if that's whats happening here, but that's how you do it everywhere else in Perl so it would make sense to do it on strings too.
- philh 11y agoPerl6 doesn't do this. A function doesn't know if it's being called in scalar or list context. (I think there might be some movement towards letting a function detect sink (void) context. Then functions with both side effects and return values don't need to calculate the return values when they aren't needed.)
- kbenson 11y agoThere's a specific reason why length was deprecated, and the error, which gives alternatives, may give some insight: # perl6 -e 'my $string = "foo"; say length $string;' ===SORRY!=== Error while compiling -e Undeclared routine: length used at line 1. Did you mean 'elems', 'chars', 'graphs', 'codes'? Perl 6 tries to get unicode right. In doing so, it makes a distinction between characters, elements, codepoints and graphemes. At certain points, you may want one or the other of these. When asking for the length of a string, what are you really asking for? The number of characters? Visible characters, or unicode combining characters? What if it's really the number of bytes in the string you need? Perl 6 makes it easy to determine all of these things in a clear manner. In doing so, it becomes obvious that "length" is ambiguous due to both its common specific role in other languages, and it's meaning when viewed in the context of these different needs. If you want individual characters, you split the string, if you want access to the underlying data, you call .encode to get the encoding you expect, or you stick it into a Buf or Blob type if you don't actually need string semantics. (Corrections welcome, I could be wrong at any number of points above).
- spathi_fwiffo 11y agoThat does shed some insight into why a generic Length is missing, but I agree with the sentiment that it is counter intuitive. It does not read right on a quick scan, and I think this is especially important in softly typed languages. If I see string.chars I expect to see a bucket of char, and not the count of chars. That name needs to be more explicit.