rakuguide icon indicating copy to clipboard operation
rakuguide copied to clipboard

Confusion over "mutators" in section 4

Open MattOates opened this issue 8 years ago • 4 comments

http://perl6intro.com/#_functions_and_mutators this section is really quite confusing and has indeed caused confusion already https://www.reddit.com/r/perl6/comments/3vjuex/how_to_easily_tell_the_difference_between_a/

MattOates avatar Dec 05 '15 21:12 MattOates

I was thinking of enhancing this section by replacing it with: https://gist.github.com/hankache/66f43377f5a6393a11646beed748c0cc

@MattOates would it become less confusing? @lizmat @zoffixznet can you kindly review?

hankache avatar Feb 25 '18 22:02 hankache

Looks more or less right, depending on how pedantic you want to get; like "The number 3 [...] will never change over time" is not quite true for Rakudo:

<Zoffix___> m: BEGIN 3 does role { method Bridge { 42 } }; say 3 + 2e0
<camelia> rakudo-moar 639c6da0c: OUTPUT: «44␤»

The references explanation is very different from how all that stuff fits into my brain—not implying that it's wrong, just different.

I don't think in terms of mutable-immutable references (that's reminiscent of Rust's ownership), but in terms of containers. my $a = 42; $a = 42 works, because there's a Scalar container and I'm storing a new value in that container. my $a := 42; $a = 42 throws, because there's no container, so there's nothing to store the new value into. More in my containers article: https://perl6advent.wordpress.com/2017/12/02/


The one thing I do see as potential for confusion is sentence "Trying to change the value of a constant leads to an error" since it implies the constant offers some sort of mutability protection, but it doesn't. If I stick a container or a mutable object into a constant, I can change the value all I want:

<Zoffix___> m: constant FOO = $ = 42; say FOO; FOO = 100; say FOO
<camelia> rakudo-moar 639c6da0c: OUTPUT: «42␤100␤»
<Zoffix___> m: constant FOO = [<a b c>]; say FOO; FOO[1] = 'meows'; say FOO
<camelia> rakudo-moar 639c6da0c: OUTPUT: «[a b c]␤[a meows c]␤»

A constant is very much like a variable that doesn't auto-give a container (like my \foo) except its initializer is run at compile time. So it's fairly easy to get a constant isn't actually constant.

zoffixznet avatar Feb 25 '18 23:02 zoffixznet

@zoffixznet I think you're right. This whole mutable/immutable feels alien to Perl 6. I should find a way to explain containers in a friendly manner to people new to Perl 6.

Thank you for your feedback.

hankache avatar Feb 26 '18 19:02 hankache

I suggest renaming the 4 section into "Methods and mutators"

interlab avatar Jul 09 '18 15:07 interlab