Some perplexities on S06
Today I was reading S06 from http://dev.perl.org/perl6/doc/design/syn/S06.html, and I have some perplexities on what's written there. 1) it's written: $pair = :when; doit $pair,1,2,3; # always a positional arg doit *$pair,1,2,3; # always a named arg but also: To pass pairs out of hash without their being interpreted as named parameters, use doit %hash:p,1,2,3; doit %hash{'b'}:p,1,2,3; instead. I interpret the last sentence as meaning that: $hash=:a<2>; doit %hash; would be equivalent to: doit :a<2> which contradicts what is written earlier... I'd assume I'd have to write: doit *(%hash); for the pair to be used as a named argument. What am I missing? 2) Consider: doit %*hash; # 1 arg, the global hash doit *%hash; # n args, the key-value pairs in the hash I think I'll use a lot of C-t while writing Perl6 code... 3) What does the second line in: push @array, :a<1>; say *pop(@array); mean? I'd parse it as 'say (*pop)(@array)', that is, call the global 'pop' subroutine. But the text around this example seems to imply that it is to be parsed as 'say *(pop(@array))', i.e. execute 'pop' and splat the result. What gives? 4) It's written: (Conjectural: Within the body you may also use exists on the parameter name to determine whether it was passed. Maybe this will have to be restricted to the ? form, unless we're willing to admit that a parameter could be simultaneously defined and non-existant.) Ok, this is 'conjectural', but... if i don't pass a parameter in an invocation, and that parameter had a defined default, that it would be both defined and non-passed... here I'm sure I'm missing something 5) while talking about pipes, it says that (0..2; 'a'..'c') ==> my @;tmp; for @;tmp.zip { say } produces [0,'a'] [1,'b'] etc. Right. But then it says: for @;tmp.each -> $i, $a { say "$i: $a" } as far as I understand, this should say 0: undef 'a': undef etc., since 'each' is visiting the multidimensional array 'by rows', and producing 1 value at a time, which when bound to the 2-ary signature would leave the $a parameter without a value, and so undef (I'm assuming that pointy sub positional parameters are implicitly optional, otherwise that statement would be a run-time error). I'm pretty sure I don't know what 'each' does... 6) It's written: Every lexical scope gets its own implicitly declared @; variable, which is the default receiver. To me, that is a variable with a null name ('@;' being a twigil). Should it not be @;_ ? I'll continue reading... -- Dakkar - GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88 signature.asc Description: OpenPGP digital signature
MMD and redefinition
In Perl5, if I do: sub foo {return 1} sub foo {return 2} print foo(); I get a redefinition error, and a '2' on STDOUT. Can I assume this will be the same in Perl6? i.e. can I write a test for pugs to check this? Moreover: sub foo(Num $a) {return 1} sub foo(Str $a) {return 2} print foo(1),foo('a'); should have the same effect? Printing 2 twice? I don't expect MMD on non-multi subs. -- Dakkar - GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88 signature.asc Description: OpenPGP digital signature
(multi)subroutine names
Say I have: multi sub foo(Array $a,Int $b) {...} multi sub foo(Hash %a, Int $b) {...} and I want to (distinctly) wrap each multisub, say for testing, or AOP, or whatever. How do I get the two different code references? As far as i can gather from the Apocalipses and Synopses, there should be a way to express the "long name", and use that to get the ref. Is there any proposed syntax for this? (I ask about syntax because the semantic seems pretty clear...) -- Dakkar - GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88 signature.asc Description: OpenPGP digital signature
Re: (multi)subroutine names
Rod Adams wrote: > It used to be > >&foo >&foo > > but I seem to recall that there was a mild change that occurred. Or > maybe I'm thinking about the adding of the colon for operators. I'm not > certain, but it's something very close to the above. Well, it doesn't seem ambiguous to me. Operators would be in the form &infix:<-> . But then, I don't design grammars... If enough people think this syntax could work, I'll add "unspecced" tests to pugs for it. > In my mind, the more interesting question is what does &foo without the > <> specifiers return when foo is multi? I see the following three options: > > 1) undef/error, since there's no single sub called foo, and multi's need > a postfix <>. At first I thought this was the right way, but then... > 2) the name 'foo', and then performs in effect call by name when > deferenced, using the dereferencing scope to determine which flavors of > foo are available at this time, and dispatching accordingly. > > 3) a dispatch table of all the foo's currently in scope at the time the > reference is made. ...one of these is actually necessary. Example: - I write a module that exports a 'foo' (a sub) - You use my module, and use that '&foo' to get a ref, which you subsequently use as you please - I then change my module so that 'foo' is a multi-sub (this is an implementation detail, not an API change) - Your code *must not* break so we can't return undef. Your #3 seem useful (for introspection, at least), but what about: multi sub foo(Array $a) {return 1} multi sub foo(Hash $a) {return 2} my $foo_A_H=&foo; multi sub foo(Str $a) {return 3} print $foo_A_H.('a'); should that print 3? Or coerce 'a' into ['a'] and print 1? What about (thinking aloud): my $foo_static=&foo; my $foo_dynamic:=&foo; where the first one has "snapshot" semantic, as in your #3, and the second one has 'name' semantic, as in your #2, in the same way that: my $a=1; my $b=$a; my $c:=$a; $a=2; print $b,$c; prints 1 2 ? -- Dakkar - GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88 signature.asc Description: OpenPGP digital signature