in reply to Re: Porting (old) code to something else
in thread Porting (old) code to something else
Of course I can :)
In perl6 the opening paren (() of a function cannot be separated from the function by whitespace:
Legal in perl5: my $foo = foo( 1 ); # A my $foo = foo(1); # B my $foo = foo (1); # C my $foo = foo ( 1 # D );
For reasons I explain in my style guide MY preference is C. Perl6 only supports A and B.
This is only an example. The syntax rules go deeper than that, causing the dot (.) to be special too, so that
my $foo = $object->method (1) ->method (2) ->method (3);
chaining as allowed in perl5 (break to a newline wherever you like), would NOT translate to
my $foo = $object.method(1) .method(2) .method(3);
This is bad (IMHO), see:
$ cat t.pl use v6; class C { has Int $.x is rw = 0; method foo (Int $i) { $!x += $i; return self; } method bar (Int $i) { $!x -= $i; return self; } } C.new().foo(1).bar(2).say; $ perl6 t.pl C.new(x => -1) $ cat t.pluse v6; class C { has Int $.x is rw = 0; method foo (Int $i) { $!x += $i; return self; } method bar (Int $i) { $!x -= $i; return self; } } C.new() .foo(1) .bar(2) .say; $ perl6 t.pl ===SORRY!=== Error while compiling t.pl Two terms in a row at t.pl:11 ------> ⏏.foo(1) expecting any of: infix stopper infix or meta-infix statement end statement modifier statement modifier loop
This is because a leading dot (after whitespace) will see the next word as method on the current topic ($_) instead of as a method on the previvious "thing". As the previous line has no trailing semi-colon, I would prefer the default to be different. The perl6 core dev people state it is possible with a backslash:
cat t.pl use v6; class C { has Int $.x is rw = 0; method foo (Int $i) { $!x += $i; return self; } method bar (Int $i) { $!x -= $i; return self; } } C.new()\ .foo(1)\ .bar(2)\ .say; $ perl6 t.pl C.new(x => -1)
But do you want your code to look ugly like that? I do not!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Porting (old) code to something else
by BrowserUk (Patriarch) on Feb 16, 2015 at 16:56 UTC | |
Re^3: Porting (old) code to something else
by BrowserUk (Patriarch) on Feb 17, 2015 at 12:52 UTC | |
by Tux (Canon) on Feb 17, 2015 at 13:29 UTC | |
by BrowserUk (Patriarch) on Feb 17, 2015 at 14:16 UTC |