xwahaa
Last Updated: February 25, 2016
·
515
· maandree
Ab927485bccfcf35ba4583723876063f

Objects in object-oriented languages suck (usually)

Consider the following class in Java:

public class Class
{   private Object value = null;
    public void set(Object value)
    {   this.value = value;
    }
    public Object get()
    {   return this.value;
    }
}

A simple class those objects just store a value.
Now consider that you want to be able to set
listen on changes:

public class Class
{   private Object value = null;
    private ArrayList<UpdateListener> listeners = new ArrayList<>();
    public interface UpdateListener
    {   void updated(Class object, Object value);
    }
    public void addListener(UpdateListener listener)
    {   this.listeners.add(listener);
    }
    public void set(Object value)
    {   this.value = value;
    }
    public Object get()
    {   return this.value;
    }
}

public class Listener implements Class.UpdateListener
{   {   someInstanceOfClass.addListener(this);
    }
    void updated(Class object, Object value)
    {   System.out.println(object.toString() + ": " + value.toString());
    }
}

Well, this sucks, and it would be a real mess if we add this for
each and every function in a large class. In, for example, C we
can do without listeners, and it would be immensity more
powerful:

typedef struct _class_t
{   void* value;
    void (*set)(struct _class_t* this, void* value);
    void* (*get)(struct _class_t* this);
} class_t;

static void _set(class_t* this, void* value)
{   this->value = value;
}
static void* _get(class_t* this)
{   return this->value;
}
class_t* Class()
{   class_t* this = malloc(sizeof(class_t));
    this->value = null;
    this->set = _set;
    this->get = _get;
    return this;
}

void add_listener()
{   void (*old_set)(class_t* this, void* value) = someInstanceOfClass->set;
    void new_set(class_t* this, void* value)
    {   printf("%li: %li", (long)(void*)this, (long)value);
        old_set(this, value);
    }
}

Languages that lets you redefine functions let the programmers do lets,
but alo provides immense potential in respect to extensibility.

Say Thanks
Respond

9 Responses
Add your response

13852
0 rm zu mj7jof6znkrw3quhu9suuaxjkkm26euhhqty70rmpxbegjg8ye zrrbv5evsri bnozmkd

In c# its one line of code

over 1 year ago ·
13853
0 rm zu mj7jof6znkrw3quhu9suuaxjkkm26euhhqty70rmpxbegjg8ye zrrbv5evsri bnozmkd

In c# its one line of code

over 1 year ago ·
13854
Ab927485bccfcf35ba4583723876063f

Really, I have not touched C♯ in “eons”?

How would you replace a function/method
of an object or class in C♯ and call the previous
implemention from the new implement in just
one statement?
It is possible with some trickery in Python that
require that you write a couple of things in before-hand.

over 1 year ago ·
13886
Ca3dc3f93730afb41d6753d8bf010a38

In Ruby, the equivalent would be:

class Foo
  attr_accessor :property_name
end
over 1 year ago ·
13887
Ab927485bccfcf35ba4583723876063f

The idea is not to have properties, the idea is to be able to redefine any function or method, so that you as a user of a library or as a user of program, can extend the program or library ad infinitum.

A very simple program I have that uses this idea is https://github.com/GNU-Pony/gates-of-tartaros

over 1 year ago ·
13932
2c3ff36a213bc5374f17c8ff700818ea

Use abstraction, inheritance and generics. Read about SOLID principals.

Also meta programming in Ruby and similar languages

If you want don't like OOP stick to procedural programming or learn a functional programming language (Erlang, F#, LISP or Scala).

over 1 year ago ·
13933
Ab927485bccfcf35ba4583723876063f

@jgoode

Why would I not know I about those things?
Especially SOLID, is there seriously anyone who don't?

I do not feel that you have anything concrete
to tell me so I cannot really reply.

over 1 year ago ·
13943
78d80e8e8c1467d53ad147439e90ed54

"the idea is to be able to redefine any function or method, so that you as a user of a library or as a user of program, can extend the program or library" My opinion: with great power comes great responsibility.

you patch a function foo in a framework bar. then a security update comes and changes foo in the new release you update the framework but you dont get the update.

Also you can easily end up with a crazy stacktrace that doesn't make sense and it feels like its a framework bug but its because the new release changed the foo func you patched.

I dont say i am against it because it can be really handy, im just saying is not that easy to just "lets cowboy this piece of code and everything will be good".

over 1 year ago ·
13945
Ab927485bccfcf35ba4583723876063f

Indeed it should be used with care and most often you will wrap a function with functionally rather than reimplementing it. Of course, if a library provides functionally that can be done by redefining a function you should use the library's functionally.

Crazy stuff can also happen if you load multiple plugins for a program that defined the same function, because one may decide to undo its wrapping, which could be required (depending on the language) before a plugin could be reloaded with an updated version.

It can be used to do want a library have not thought of enabling you to do. But it can also be used to let a configuration script extend the program; if your program is not that big this is can be used to keep to code short and clean.

over 1 year ago ·