The Artima Developer Community
Sponsored Link

Programming in Scala Forum
"A class and its companion object can access each other’s private members."

1 reply on 1 page. Most recent reply: Nov 27, 2016 7:42 AM by Daniel Barclay

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
    Next Topic
Flat View: This topic has 1 reply on 1 page
Twan van der Schoot

Posts: 1
Nickname: 80043
Registered: Dec, 2011

"A class and its companion object can access each other’s private members." Posted: Apr 23, 2016 5:01 AM
Reply to this message Reply
Advertisement
In the PDF version of the 3rd edition of Programming in Scala in §4.3 2nd paragraph last sentence says:

"A class and its companion object can access each other’s private members."

This is in its generality false. A class can see (private) members of a companion object, but a companion object can't access the members of the associate class.

Moreover, you need to import the companion object's member names explicitly using e.g. "import X._" before you can access the object's members from the associated class or from an instance of that class.

But I fail to see how an object can have meaningful access to class members, which are essentially always members of an instance. If there is more than one instance, which instance to choose?

1. I think this line should be corrected in the book because it is very confusing. Only if you know Java and the behavior of a static in a class well enough, you understand why it can't work.

2. I failed to find an explanation in the book never that you need to import the names from the object into the class, before you can access them. It would have been nice to have that pointed out in the book.

At this moment only googling and experimenting (and my experience with Java) made clear what the limitations on the purported mutual accessibility of (companion) object and class members actual are.


Daniel Barclay

Posts: 5
Nickname: daniel7
Registered: Sep, 2016

Re: "A class and its companion object can access each other’s private members." Posted: Nov 27, 2016 7:42 AM
Reply to this message Reply
You might want to also report that via the errata pages at
http://booksites.artima.com/programming_in_scala_3ed/errata.

Flat View: This topic has 1 reply on 1 page
    Next Topic Topic: 5 Tips to Get More Accurate Results on Google Search

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use