in reply to Re^8: Small Perl 6 discoveries II, Rats
in thread [Perl6] Small discoveries I, __DATA__

You can give $x a type, but prepare to be disappointed.
> my Rat $x = 0.1; > $x *= 0.1.FatRat; Type check failed in assignment to $x; expected Rat but got FatRat (Fa +tRat.new(1, 100))

Replies are listed 'Best First'.
Re^10: Small Perl 6 discoveries II, Rats
by holli (Abbot) on Oct 25, 2017 at 01:43 UTC
    Yeah, well. That is to be expected as it is akin to assigning an int64 to an int32. What surprises me is that
    perl6 -e "my FatRat $r = 0.1"
    throws the same error.


    holli

    You can lead your users to water, but alas, you cannot drown them.
      First, Rats aren't FatRats and vice-versa. (If we need to discuss why, we'll need to start with Larry's comment that "we've intentionally steered clear of any sort of numeric tower"; and then dig into what p6curious meant when they followed that up with "its one of the things that has impressed me the most about perl6"; and then consider Rats and FatRats in particular.)

      Second, P6 has a nice feature that directly addresses this. It has been implemented for routine parameters but not yet `my` declarations:

      sub fails-if-passed-Rat (FatRat $r) { say WHAT $r } ; sub works-if-passed-number (FatRat() $r) { say WHAT $r } ; fails-if-passed-Rat 1.0 ; # typecheck failure works-if-passed-number 1.0 ; # coerces 1.0 to a FatRat my FatRat $r = 1.0 ; # typecheck failure my FatRat() $r = 1.0 ; # should say "not yet implemented"

        First, Rats aren't FatRats and vice-versa.
        They're both Rational, but that's still not very useful.
        > my Rational $x = 1.111111111111111111111; 1.11111111111111111604544 > $x *= 0.1; Type check failed in assignment to $x; expected Rational but got Num ( +0.111111111111111e0)
        The argument type-coercion is cute, but there doesn't seem to be any way to set the precision.
        > sub foo(FatRat() $x) { return $x }; foo(pi).nude (355 113) > pi.FatRat(1e-8).nude (103993 33102)
        And I don't know how to interpret this message...
        > my FatRat() $x = 0.1; ===SORRY!=== Error while compiling: Coercion FatRat(Any) is insufficiently type-like to qualify a variable ------> my FatRat() $x⏏ = 0.1; expecting any of: constraint
Re^10: Small Perl 6 discoveries II, Rats
by syphilis (Archbishop) on Oct 25, 2017 at 02:54 UTC
    Type check failed in assignment to $x; expected Rat but got FatRat

    At least that Rat appears to be immutable ;-)

    I need to study the documentation and spend some time working through it.

    Cheers,
    Rob
      I do think studying the doc and working through it is the way to go but in the meantime, for anyone else reading along, I may be missing a joke here (given Rob's wink smiley), but to be humorless but hopefully clear, all Rats are immutable, as explained in my ancestor comment.
Re^10: Small Perl 6 discoveries II, Rats
by raiph (Deacon) on Oct 27, 2017 at 19:25 UTC
      What, then, should I declare as the type of my variable? There seems to be no good choice.