develooper Front page | perl.perl6.users | Postings from January 2019

Re: Bad syntax check

Thread Previous | Thread Next
From:
Brad Gilbert
Date:
January 3, 2019 00:00
Subject:
Re: Bad syntax check
Message ID:
CAD2L-T3ENsknATL60z61wq+xjC5u_iKC3eU2wvOKrJtipaXJTw@mail.gmail.com
You can only list one type as a return type.

If there were a hypothetical Tuple type:

    sub AddThree( Int $a, Int $b, Int $c --> Tuple[Str, Int] {
        my Int $d = $a + $b + $c;
        return Tuple[Str,Int].new( "a+b+c=", $d );
    }


    my Int $X = 0;
    my Int $Y = 0;
    my Str $Z;

    Tuple[Str,Int].new( $X, $Y, $Y ) = AddThree( 1, 2, 3 );
    say "X = <$X>\tY = <$Y>\tZ = <$Z>";

 There could be a circumfix operator to generate the Tuple objects.

You are dealing with Lists, which don't have a type specifier for their
elements.

On Wed, Jan 2, 2019 at 4:51 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/2/19 2:27 PM, ToddAndMargo via perl6-users wrote:
> > On 1/2/19 2:11 PM, Simon Proctor wrote:
> >> Have you tried defining your return values in the signature?
> >>
> >> sub AddThree( Int $a, Int $b, Int $c --> Int) {...}
> >>
> >> With this the compiler knows what your function is supposed to return
> >> and can earn you in advance.
> >
> > I did and it blew up in my face so I stopped doing it.
>
> For example:
>
> $ cat RtnBooBoo2.pl6
> #!/usr/bin/env perl6
>
> sub AddThree( Int $a, Int $b, Int $c --> Str, Int ) {
>     my Int $d = $a + $b + $c;
>     return ( "a+b+c=", $d );
> }
>
> my Int $X = 0;
> my Int $Y = 0;
> my Str $Z;
>
> ( $X, $Y, $Y ) = AddThree( 1, 2, 3 );
> say "X = <$X>\tY = <$Y>\tZ = <$Z>";
>
>
>
> RtnBooBoo2.pl6
> ===SORRY!=== Error while compiling /home/linuxutil/./RtnBooBoo2.pl6
> Malformed return value (return constraints only allowed at the end of
> the signature)
> at /home/linuxutil/./RtnBooBoo2.pl6:3
> ------> AddThree( Int $a, Int $b, Int $c --> Str⏏, Int ) {
>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About