|
|
Subscribe / Log in / New account

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 16, 2015 4:19 UTC (Thu) by raiph (guest, #89283)
In reply to: An interview with Larry Wall (LinuxVoice) by juliank
Parent article: An interview with Larry Wall (LinuxVoice)

You've misunderstood Perl 6 "private/public" semantics. I'll try to clear that up as I respond to your suggestions about syntax.

Private
=======

Almost all the Perl 6 code I've seen uses the `!` symbol for private attribute declarations. I've read many Perl 6 coders say they prefer to use it.

But the `!` is optional. One can write as you suggest for the private attribute except missing out the word `private`:

class Foo { has Task @dependencies; # declare a private attribute that's an array of Tasks }

Public
======

A public accessor is a method, not an attribute.

A method is called using `.` syntax. Thus, assuming a public accessor method called `done` has been written for a class `Foo`, one might write code such as `say Foo.new.done` and it'll work.

The typical way to get a public accessor method written is to ask Perl 6 to automatically generate it for you:

class Foo { has Bool $.done; # declare attribute, generate public accessor }

The `.` is there in the declaration to remind one of a method call.

All methods called with `.` are public method calls so I'm not seeing the benefit of using the word 'public'. And dropping the `.` would be a big backward step imo.

So...
=====

Use `.` for declaring both a private attribute and public accessor method.

Then use the same `.` for calling the method -- with associated semantics/performance (run-time method resolution that only the JIT can optimize).

Use `!` (or simply omit the `.`) for declaring a private attribute.

Then use the same `!` (or not) for reading/writing the private attribute -- with associated semantics/performance (compile-time direct attribute access that's inlinable by an AOT optimizer).

---------------------------------------------

> Using ~ for string concatenation ... is weird.

I still recall the first time Larry explained his choice. It looks like a piece of string. If this mnemonic doesn't instantly make you go "ahhh", well, you're weird. :P

---------------------------------------------

> A good language design should take best practices, and not re-invent the wheel everywhere.

Sure. It sounds like we just disagree on what best practices to focus on and when innovation is a good thing.


to post comments

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 16, 2015 17:51 UTC (Thu) by bronson (subscriber, #4806) [Link] (14 responses)

Wow, for noewcomers it sounds like public/private is fairly unreadable/unintuitive. You might have made juliank's point better than juliank did.

~ may look like a string, but it doesn't look like a string concatenator to me. And ~~ definitely doesn't look like a matching operator. But then, it took me a long time to get used to =~.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 17, 2015 18:26 UTC (Fri) by raiph (guest, #89283) [Link] (13 responses)

> Wow, for noewcomers it sounds like public/private is fairly unreadable/unintuitive.

Really? Once again:

has $foo; # declare attribute
say $foo; # read attribute

has $.foo; # declare attribute and method
say $.foo; # call method

Does that really not make sense to you?

Imo, this should be readable and intuitive for most folk who are used to using `.` for method calls. And forgetting my opinions and shoulds, in actuality I've not seen any newcomers talk about getting tripped up by this when learning Perl 6.

> ~~ definitely doesn't look like a matching operator. But then, it took me a long time to get used to =~.

Yeah, I always disliked =~ and I've heard others say the same.

I'm OK with ~~. Given the usual meaning of ~, it does approximately match a matching operator.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 17, 2015 20:24 UTC (Fri) by sorpigal (guest, #36106) [Link] (12 responses)

Some people, especially people who are down on Perl, are anti-sigil. Sigils outside of Perl are seen as hard to read and having more of them is obviously bad. I view the negative reaction you're seeing here as simple anti-sigilism.

Sigils are a good thing. They make code easier to read (and understand). If you find this isn't true *you're just not using enough of them*.

As for concatenation, ~ makes as little sense as . or and is no harder to type than +. It only seems weird because it's young. If you want weird, try := for an assignment operator! It's ugly, hard to type and makes no sense... to me, because I don't use many languages that require it. All syntax is intuitive given time. Meaning comes from usage, not the other way around.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 17, 2015 20:29 UTC (Fri) by juliank (guest, #45896) [Link]

:= is the common way to write a definition in mathematics. It makes sense for definitions, but certainly not that much for re-assignment.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 20, 2015 10:13 UTC (Mon) by leifbk (guest, #35665) [Link] (6 responses)

I was a carpenter before I started to program in Turbo Pascal in the 80s. It was easy for me to visualise the assignment operator := as a saw with a handle, while the test for equality = is similar to a yardstick.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 20, 2015 11:56 UTC (Mon) by juliank (guest, #45896) [Link] (5 responses)

I don't understand you and others doing this.

I just remember := as a token. I do not try to relate syntax to real world elements. Why would I do that when I can just remember := in a few milliseconds.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 20, 2015 14:44 UTC (Mon) by Wol (subscriber, #4433) [Link] (2 responses)

> I don't understand you and others doing this.

Because you are not him!

You're coming over a bit as "my way is the only way" - the exact opposite of Perl's TMTOWTDI. Be careful - that attitude can get you into trouble.

Most people like GUIs. I hate them. But then, I'm happy to accept that other peoples' brains are wired differently. Expecting everybody to be a clone of yourself won't get you very far.

(Oh - and why is it a good idea to assume a linguist can't design a language? If he's a professional doing it for the pay, I'd be inclined to agree, but if he's an amateur being paid to do what he loves, then ...)

Cheers,
Wol

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 20, 2015 15:08 UTC (Mon) by juliank (guest, #45896) [Link] (1 responses)

> > I don't understand you and others doing this.
> Because you are not him!
> You're coming over a bit as "my way is the only way" - the exact opposite of Perl's TMTOWTDI. Be careful - that attitude can get you into trouble.

I did not mean this in a negative way. I'm just curious that some people do this, and other's don't. I would have never thought about mnemonics, so this is a new and interesting thing for me.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 20, 2015 20:16 UTC (Mon) by Wol (subscriber, #4433) [Link]

I know I repeatedly refer to Dick Feynmann, but read about him and counting numbers in your head. Apparently, if you try to count 60 seconds, it won't be a minute, but it WILL be consistent - when I tried it it always seemed to take me about 47 seconds.

Anyways, Dick and his undergrad mates did this, and Dick was amazed when one of them would quite happily hold a conversation while doing it. Turns out this guy counted by "seeing" a ticker-tape go by. What he couldn't do was watch a TV program while counting.

Dick, on the other hand (like, I think, most people including me), counted silently in his head and had no trouble watching - then recalling - the TV but there was no way he could hold a conversation while counting.

Different people are most definitely wired differently. And that is now one of the big tensions with "linux" - all these guis, gnome, kde, X even, are all "against the spirit" of Unix. It's a pretty safe bet all the original Unix luminaries were word-smiths, not visual people. I'm not saying that's good or bad, just that it *is*. I know all this emphasis on guis alienates me.

Cheers,
Wol

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 22, 2015 19:20 UTC (Wed) by raiph (guest, #89283) [Link] (1 responses)

I suspect you related `:=` to something that already exists, eg its use in conventional math notation.

If not, why, regarding the "private/public" feature discussed elsewhere, would you not be able to just remember `!` as a token that means private?

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 22, 2015 21:17 UTC (Wed) by mathstuf (subscriber, #69389) [Link]

Think of it as the bubble over characters' heads in Metal Gear Solid when you try to peek at them.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 21, 2015 7:31 UTC (Tue) by jezuch (subscriber, #52988) [Link] (3 responses)

> If you want weird, try := for an assignment operator! It's ugly, hard to type and makes no sense...

Oh, to the language designers a couple of decades ago (before C, basically), it made perfect sense. Assignment is definitely *not* the same thing as equality in mathematics. The latter is a declaration of equivalence of two symbolic expressions; the former is an instruction to perform a side effect (in imperative languages). So it makes sense to use a different symbol. Now we're all brainwashed by C so when we see "if (a = b)" something inside us screams "this is a mistake!!!" ;)

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 21, 2015 15:48 UTC (Tue) by dashesy (guest, #74652) [Link]

Now we're all brainwashed by C so when we see "if (a = b)" something inside us screams "this is a mistake!!!" ;)
More like, that "if (a = b)" bug now has its signature engraved in our genome, and the immune system shows symptoms of allergy including twitching in the eyes and fever.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 21, 2015 16:34 UTC (Tue) by sorpigal (guest, #36106) [Link] (1 responses)

I'm well aware of where := comes from; it makes total sense and it's clearly a logically superior choice to plain =. I chose it as an example because I expected everyone to be familiar with it. The point is that what's normal is what we normally do or think and what's weird is what's foreign. There's a history of using + and to a lesser extent . for concatenation, so of course those seem reasonable. I expect that after six months of use ~ will have become easy to read and it would be jarring to use another operator when switching to another language.

Perhaps it's not clears, so I'll emphasize: I mean this as much for sigils as other syntax choices.

Private/public attributes/methods (was: An interview with Larry Wall (LinuxVoice))

Posted Jul 22, 2015 20:16 UTC (Wed) by Wol (subscriber, #4433) [Link]

And for me, the natural concatenation operator is ":"

I = 10
I = I : "00"
I = I * 5
PRINT I

Which gives me "5000" :-) Oh the joys of untyped variables :-)

Cheers,
Wol


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds