Raku / old-design-docs

Raku language design documents
https://design.raku.org/
Artistic License 2.0
124 stars 36 forks source link

Should colons be allowed in method names? #106

Closed MadcapJake closed 8 years ago

MadcapJake commented 8 years ago

In working out a script that gathers completions, I asked a question on IRC:

Can a :: sigil appear in names of methods/subs?

Initially the response was "that doesn't even make sense" but was followed up by:

class Foo { method foo::bar () { say 665; }; }; Foo.new."foo::bar"(); #-> 665

I don't understand what the usefulness of this feature is. I've done some skimming through S02#names and I can't find any mention of "you can use colons in method names but you'll need to access them via quotes" (though I could've missed it).

I confess, I don't quite have a firm grasp on the :: sigil and package lookup, but why would this be a useful feature?

awwaiid commented 8 years ago

The feature isn't really that ':' can be in a method name -- it is that ANYTHING can be in a method name. An illustrative example is using ' ' (a single space) as a method name.

class CrayCray {
    my $method = method (|args) { 42 };
    $?CLASS.^add_methmethod(' ', $method);
}
say CrayCray.new." "() # outputs "42"

You can see here that I wasn't able to declare this method using the normal syntax. So maybe another question is why we are able to use the ":" in the method name without having to go out of our way to make it look fancy. I guess... because it's not ambiguous because you can't put namespaces in normal method declarations.

niner commented 8 years ago

I'm actually for allowing as much as possible in method names. Not so much for Perl 6 code as method names that have to be quoted all the time aren't that useful, but for interop with other languages which may have strange naming conventions.

MadcapJake commented 8 years ago

@niner I'm all for allowing any kind of character in method names but I think it's a bit confusing to just let colons occur in method declarations without needing to add them via a special method (ala the space method @awwaiid posted). At the very least, somewhere along the way, a warning/error should be issued as the :: syntax is meant to be a specific syntactic/semantic operator/sigil in a few different contexts (all of them revolving around names of things).

LLFourn commented 8 years ago

:: is a required feature in method names because of the way coercions work.

http://docs.perl6.org/language/functions#Coercion_Types

A single ':' is even more necessary because of method foo:sym in grammar actions.

On Mon, Jan 18, 2016 at 5:44 AM Jake Russo notifications@github.com wrote:

@niner https://github.com/niner I'm all for allowing any kind of character in method names but I think it's a bit confusing to just let colons occur in method declarations without needing to add them via a special method (ala the space method @awwaiid https://github.com/awwaiid posted). At the very least, somewhere along the way, a warning/error should be issued as the :: syntax is meant to be a specific syntactic/semantic operator/sigil in a few different contexts (all of them revolving around names of things).

— Reply to this email directly or view it on GitHub https://github.com/perl6/specs/issues/106#issuecomment-172363415.

MadcapJake commented 8 years ago

@LLFourn thanks for the linkage! So in that situation they are allowed because they need to match how the type is written. Could you provide a link or explanation wrt to grammar actions, I don't see anything like that in the grammar actions section.

It still feels like some discussion could be had regarding whether they should be declared the same or if maybe there could be a pertinent error message given if you tried to call one, e.g.,

"Cannot locate &bar, this method call contains :: perhaps you meant to enclose the call in quotes."

LLFourn commented 8 years ago

https://github.com/rakudo/rakudo/blob/nom/src/Perl6/Actions.nqp#L1570

I haven't tried but it probably works in vanilla Perl 6 too. (btw we truely need to work on the grammar docs).

If you mean that error messages could be improved when invoking methods with "::" that don't exist I can agree with you. Keep in mind that qualified method calls are a thing:

Lloyds-iMac:~ llfourn$ p '"foo".Mu::say()'
foo
Lloyds-iMac:~ llfourn$ p '"foo".Mu::sa()'
Cannot find method 'sa'
  in any find_method_qualified at gen/moar/m-Metamodel.nqp line 1101
  in block <unit> at -e line 1
MadcapJake commented 8 years ago

Thanks everyone for clearing this up for me!