How to change a class' behavior from outside?

30 views
Skip to first unread message

Damien Cassou

unread,
Nov 11, 2011, 5:50:45 AM11/11/11
to Newspeak Programming Language
Hi,

I would like to change a class' behavior from outside this class,
something similar to Context Oriented Programming or Aspect Oriented
Programming.

I tried creating a subclass of this class with the same name, like in
the ECOOP paper:

public class Shape = super Shape ()()

Unfortunately, bug https://bitbucket.org/rmacnak/newspeak/issue/22/compiler-does-not-handle-nontrivial
prevents me from doing so.

Is there another way?

Thank you

Ryan Macnak

unread,
Nov 11, 2011, 8:41:29 AM11/11/11
to newspeak...@googlegroups.com
Indeed, superclass clauses are currently limited to a simple name optionally followed by a message. The recommended work-around is to create a slot initialized to your non-trivial expression of choice and use that slot as the superclass.

class MyModule = (|  SuperFoo = super Foo. |)(
 class Foo = SuperFoo ()()
)

BlocklessCombinatorialParsing is a module that uses this, so that would be the place to look for examples.

Damien Cassou

unread,
Nov 11, 2011, 9:26:41 AM11/11/11
to Newspeak Programming Language
Thank you very much. For the record, here is what I wanted to do by
"changing a class' behavior from outside". The class C defines a
module with some features and I can't change its source code. In my
module D, I want to change the behavior of some parts of C. I then
create a subclass of C and I create subclasses of some of its inner
classes (the new classes must have the name of their super class).

class C = (||) (
class C1 = (||) (
f = ( ^ 1)
) : ()
class C2 = (||) (
g = ( ^ C1 new f)
) : ()
) : ()

class D withC: CClass = (| realC = CClass. |) (
class ExtendedC = realC (|SuperC1 = super C1.|) (
class C1 = SuperC1 (||) (
f = ( ^ 2)
) : ()
) : ()
) : ()


Now, if I execute :

(D withC: C) new ExtendedC new C2 new g

I get 2 instead of 1. Which really means I can have a private version
of some classes I don't own.

Thank you

Gilad Bracha

unread,
Nov 13, 2011, 1:38:47 PM11/13/11
to Newspeak Programming Language
Hi Damien,

Tjis is known as class-hierarchy inheritance - the ability to inherit
and extend entire class hierarchies. It works, and hass been working
for a long time, with the caveat that you have to give a name to the
superclass (which will be fixed eventually). We use this in the parser
combinator library for example (BlocklessCombinatorialParsing is
designe dto be mixed in with CombinatorialParsing and override its
behavior).

Real world examples of this are not all that common. Eliot recently
had a case where he wanted to override the Debugging module so it
would better support debugging a DSL.
I am interested to know what your actual intended scenario was>

Eliot Miranda

unread,
Nov 13, 2011, 2:38:04 PM11/13/11
to newspeak...@googlegroups.com
On Sun, Nov 13, 2011 at 10:38 AM, Gilad Bracha <gbr...@gmail.com> wrote:
Hi Damien,

Tjis is known as class-hierarchy inheritance - the ability to inherit
and extend entire class hierarchies. It works, and hass been working
for a long time, with the caveat that you have to give a name to the
superclass (which will be fixed eventually). We use this in the parser
combinator library for example (BlocklessCombinatorialParsing is
designe dto be mixed in with CombinatorialParsing and override its
behavior).

Real world examples of this are not all that common. Eliot recently
had a case where he wanted to override the Debugging module so it
would better support debugging a DSL.

Indeed.  This allowed me to interpose a superclass above some general classes so that I could extend all their behaviours in one place.  The original module, ActivationMirrors, defines some ActivationMirror classes:

 class ActivationMirror onContext: ctxt = (
    class ActivationMirrorForNewspeak onContext: ctxt = ActivationMirror onContext: ctxt (
    class ActivationMirrorForSqueak onContext: ctxt = ActivationMirror onContext: ctxt (

in my module that extends this I am able to say

   class ActivationMirror onContext: ctxt = ActivationMirrorsActivationMirror onContext: ctxt (

where ActivationMirrorsActivationMirror is a binding to ActivationMirror in the original ActivationMirrors.

Hence methods on my ActivationMirror are inherited by ActivationMirrorForNewspeak & ActivationMirrorForSqueak in my module, and override those in ActivationMirrorsActivationMirror.  This is exactly what I wanted.
--
best,
Eliot

Damien Cassou

unread,
Nov 13, 2011, 3:07:38 PM11/13/11
to Newspeak Programming Language
Hi Gilad,

On Nov 13, 7:38 pm, Gilad Bracha <gbra...@gmail.com> wrote:
> I am interested to know what your actual intended scenario was>

I didn't have any scenario in mind, I'm just playing with Newspeak.

Thanks for your explanation
Reply all
Reply to author
Forward
0 new messages