Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^8: Small Perl 6 discoveries II, Rats

by raiph (Deacon)
on Oct 24, 2017 at 02:55 UTC ( [id://1201936]=note: print w/replies, xml ) Need Help??


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

the following operation converts $x from a Rat to a FatRat

That's a a mutable "container" aka a variable (a Scalar in this case) containing one immutable value (a Rat) then another immutable value (a FatRat).

# Declare a new identifier $x and bind it to a new mutable Scalar cont +ainer: my $x; # Assign a value into the Scalar: $x # An LHS reference to a Scalar returns the Scalar. = # = is assignment. The LHS decides what to do with i +t. 9.9; # Creates a new immutable Rat. # VAR macro returns what the identifier is bound to: say $x.VAR.^name; # Scalar -- the type of variable bound to $x. # All other uses of $x or a Scalar on RHS return the value assigned in +to the Scalar: say $x; # 9.9 say $x.^name; # Rat -- the type of the value assigned into the Sca +lar. $x *= 2; # This is equivalent to ... $x = $x * 2; # ... which assigns result of RHS expression into Sc +alar on LHS. say $x; # 39.6

Hth.

Replies are listed 'Best First'.
Re^9: Small Perl 6 discoveries II, Rats
by syphilis (Archbishop) on Oct 28, 2017 at 11:41 UTC
    That's a mutable "container" aka a variable (a Scalar in this case) containing one immutable value

    I really do not understand the use of the term "immutable".
    Is it perhaps meant to be some shorthand method of signifying that the numerator and denominator are co-prime ? (For this, the gmp C library documentation uses the term "canonical" - in the sense, I think, of "authoritative, standard, accepted".)
    Admittedly, the connection between the meanings of "co-prime" and "immutable" seems very tenuous to me, but it's about all that I can come up with.

    Cheers,
    Rob
      One thing's for sure -- you're confused! I apologize if the following still doesn't help and/or is aggravating.

      I really do not understand the use of the term "immutable".

      To me it means "never changes".

      There are three exceptions:

      Construction

      A data item must go from uninitialized/undefined to initialized/defined:

      my $foo := 42; # declares $foo at compile time, binds it to immutable +42 at run-time BEGIN say $foo; # (Any) $foo = 99; # Cannot assign to an immutable value

      Destruction

      Memory is of course reclaimed at the end of a process at the latest if not garbage collected earlier (when no more references to it exist).

      Referencing mutable data

      One can have a reference that is itself immutable but which refers to mutable data. (Rats aren't mutable. So please don't think "ah, maybe this is the issue". I'm just trying to be complete in elaborating on the precise meaning of "immutable" in P6.) Thus:

      my @foo := (42, $); @foo[1] = 99; say @foo; # (42 99) @foo[0] = 99; # Cannot modify an immutable Int

      If your light bulb still hasn't lit up at least a little, perhaps it really is best for you to just wait until materials about P6 sufficiently improve (maybe years? maybe never?) and you have time to enjoy exploring them. (I don't think anyone can absorb new information well unless they're enjoying the process and it feels like you're very much not enjoying this at any level. I apologize if I've got that wrong.)

      Alternatively, perhaps you could pick a line from my earlier comment in which I broke things down into tiny pieces, one that isn't clear to you, and we can then try to clear that up, and then another line, and so on?

        One thing's for sure -- you're confused!

        Thanks - I think it was just a misunderstanding of what constitutes a "rational object".

        I hope I'm correct in asserting that $x is a "rational object" if and only if $x.VAR.^name returns Rat - in which case it is impossible to assign to $x and the value that $x points to and returns cannot change (ie is immutable).
        It seems somewhat similar to the perl5 scenario where one assigns $ref = \42;
        In such a case the value of $$ref likewise cannot be altered (ie is immutable).

        I'm further wondering if it's the case (in perl6) that $x is an immutable object whenever $x.var.^name returns anything other than Scalar ... though faik there could be other exceptions ...

        Cheers,
        Rob
Re^9: Small Perl 6 discoveries II, Rats
by Anonymous Monk on Oct 24, 2017 at 15:57 UTC
    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))
      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"

      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.
        What, then, should I declare as the type of my variable? There seems to be no good choice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201936]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found