Protect parents from children

Simon Strandgaard wrote:

···

On Wed, 25 Jun 2003 13:29:31 +0000, Martin DeMello wrote:

Simon Strandgaard 0bz63fz3m1qt3001@sneakemail.com wrote:

In C++ private members is inacessable to derivatived classes.
I want the same, but with a extra twist.
Instances declared in the parent should be readonly(frozen) to the
subclass.

So that the subclass (B):

  • is allowed to read the variable.
  • is disallowed to write to the variable (raise exception).

Offhand, I’d say it wasn’t even hackable in pure ruby, because

[snip]

I am sad to hear that. Then I have to learn to live without protection.
Thanks Guy+Martin for the discussion it has been educational.

BTW: Perhaps it could be nice if you could say:

class A
def initialize
@value = [1, 2, 3]
end
subclass_freeze :value # maybe nice to have ?
end


Simon Strandgaard


([ Kent Dahl ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ (pre-) Master of Science in Technology )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

I am sad to hear that. Then I have to learn to live without protection.

This is just the proof that you've not forgotten the C++ model :slight_smile:

Guy Decoux

I am sad to hear that. Then I have to learn to live without protection.
Thanks Guy+Martin for the discussion it has been educational.

You’re welcome - it’s been educational for me too.

BTW: Perhaps it could be nice if you could say:

class A
def initialize
@value = [1, 2, 3]
end
subclass_freeze :value # maybe nice to have ?
end

This is probably hackable, with some judicious delving into Module and
hooks. But you can only freeze an object, not a variable, so it would
prevent @value << [foo] in a subclass, but not @value = bar

As a fellow C++ refugee, I’ve found that while Ruby doesn’t support all
C++'s constructs, there’s usually an equivalent way to do any given
problem, and it almost invariably feels better :slight_smile:

Actually, I’m still not perfectly clear on what you want. Is it a
variable whose value can be mutated in any methods you write inside
class A, but which client programmers writing classes that inherit from
A can only treat as a constant? How about if they call a method from A
which changes the value of @value? What if they override @initialize and
don’t call super - how does the value get set, then? And don’t forget
they can simply reopen A and add a method of their own, then call it
from B.

martin

···

Simon Strandgaard 0bz63fz3m1qt3001@sneakemail.com wrote:

C++ got erased… it must be some leftovers from Pascal ?

Perhaps it would be wise to add this issue to ???

···

On Wed, 25 Jun 2003 23:04:02 +0900, ts wrote:

I am sad to hear that. Then I have to learn to live without protection.

This is just the proof that you’ve not forgotten the C++ model :slight_smile:


Simon Strandgaard

BTW: Perhaps it could be nice if you could say:

class A
def initialize
@value = [1, 2, 3]
end
subclass_freeze :value # maybe nice to have ?
end

This is probably hackable, with some judicious delving into Module and
hooks. But you can only freeze an object, not a variable, so it would
prevent @value << [foo] in a subclass, but not @value = bar

:slight_smile:

Is it a variable whose value can be mutated in any methods you
write inside class A, but which client programmers writing classes that
inherit from A can only treat as a constant?

Yes. Any method within A is able to read+write (mutable).

Subclasses of A is only able to read (imutable).
If subclasses attempt to write they die.

How about if they call a method from A which changes the value
of @value?

Subclasses should be allowed to call a superclass-method, which does make
changes.

What if they override @initialize and don’t call super - how does
the value get set, then?

I don’t know which kind of behavier would be appropriate here?

And don’t forget they can simply reopen A and add a method of their own,
then call it from B.

I consider 'reopen’ing as a kind of subclassing. if subclass then its
readonly mode :slight_smile:

···

On Wed, 25 Jun 2003 14:22:56 +0000, Martin DeMello wrote:

Simon Strandgaard 0bz63fz3m1qt3001@sneakemail.com wrote:


Simon Strandgaard

What if they override @initialize and don’t call super - how does
the value get set, then?

I don’t know which kind of behavier would be appropriate here?

Me neither - again, note the problem of values attaching themselves to
an instance, rather than a class.

And don’t forget they can simply reopen A and add a method of their own,
then call it from B.

I consider 'reopen’ing as a kind of subclassing. if subclass then its
readonly mode :slight_smile:

I seriously doubt you can write a Ruby program capable of distinguishing
between

class A
def foo; end
def bar; end
end

and

class A
def foo; end
end

class A
def bar; end
end

In fact, if there weren’t so many ingenious people on the list, I’d go
ahead and flatly say it was impossible :slight_smile:

martin

···

Simon Strandgaard 0bz63fz3m1qt3001@sneakemail.com wrote:

On Wed, 25 Jun 2003 14:22:56 +0000, Martin DeMello wrote:

And don’t forget they can simply reopen A and add a method of their own,
then call it from B.

I consider 'reopen’ing as a kind of subclassing. if subclass then its
readonly mode :slight_smile:

I seriously doubt you can write a Ruby program capable of distinguishing
between
[snip}
In fact, if there weren’t so many ingenious people on the list, I’d go
ahead and flatly say it was impossible :slight_smile:

Well, reopening is a grey area… not strictly important to me.

What is most interesting for me, is if you such protection works with
inheritance. But I doubt its possible, when reading this thread.

Again if there is any experts, who know something we others dont, then say
something :slight_smile:

···

On Wed, 25 Jun 2003 21:45:12 +0000, Martin DeMello wrote:

Simon Strandgaard 0bz63fz3m1qt3001@sneakemail.com wrote:

On Wed, 25 Jun 2003 14:22:56 +0000, Martin DeMello wrote:


Simon Strandgaard