-2

One thing that I find most annoying about OOP is that whenever you need a new variable in a member function, and you need that this variable "attaches" the object on which this function is called, you have basically no choice but creating a new private field. This is ugly in my opinion, because this mean that the variable is initialized at object instantiation (and you can possibly never make use of it if you don't call the method that needs it), it's not hidden from the other entities that can access the private members of the object, and on top of all this means can clutter your class definition (think about C++ classes, which most often come with an header with the entire definition of the fields in it).

Speaking in the C++ lingo, I want the behaviour of the static modifier on a variable in a global function, but in member functions, and the storage must be in the object.

I don't know that many languages, but I have the feeling that in dynamic programming languages it's easier to do it. I can think of Lua: I would just add a new index in the current table. This doesn't hide the new "field" from the rest of the world, but unless you tamper the metatable, everything in Lua is public, so it is not really a problem in the Lua mindset. But the problem of initialization is addressed.

So, my question is: is there any static programming language (i.e., one in which the layout of an object is known at compile time) where this thing is possible?

And by the way, is there a neat workaround in C++ to get a similar result?

4
  • So... you want to have long-lived, per-object state without declaring it? Sounds like a bad idea. FWIW, this is rarely done in Python although it's entirely possible.
    – user395760
    Oct 18, 2012 at 22:21
  • Can you add some code that demonstrates the problem, and some pseudo-code that fixes it?
    – GManNickG
    Oct 18, 2012 at 22:23
  • Sounds like you want nested classes?
    – Steve-o
    Oct 18, 2012 at 22:33
  • @Steve-o, nested classes keep a reference to the enclosing class, not the contrary Oct 19, 2012 at 2:39

2 Answers 2

0

As the general idea in object oriented programming is to encapsulate behaviour as a set of methods (functions) and the data as a set of instance variables, this behaviour is not present in any static programming language that I can think of.

However, the idea to separate the concerns of a single class into multiple units (as to make it less monolithic) has been considered in a few languages. The idea is to separate the various concerns within the single class of object. Although the variable is not created at runtime (this would make the type non static), it can be declared in a unit separate from other units.

This is actually sort of available in C#. Although the implementation is purely cosmetic, you can declare a single class as multiple partial classes. One of the benefits is to separate concerns to avoid a single big declaration of everything encapsulated by the class (another use is code generation scenarios).

This lets you do the following:

File1:

partial class Foo {
   int X;
   void DoSomethingWithX() {
        X++;
   }
}

File 2:

partial class Foo {
   int Y;
   void DoSomethingWithY() {
        Y++;
   }
}
1
  • I don't know C#, it doesn't look exactly what I asked for but it's a nice features to make classes less monolithic, which is in fact the problem. Oct 19, 2012 at 2:38
0

You say static and by design in static languages every thing about the design of object should addressed in compile time and class can't change it self in runtime. So I don't think any static language can do such a dynamic job, and it is good since it will decrease performance, when you access a variable, in static languages compiler create code to access that variable at compile time but if such a variable (function-object-static) exist, then compiler should write an extra code to check some dynamic storage to see if a property with that name exist or not and this is only because you feel sad with current design of C++!? But on the other hand, if you do not want to do it without changing fields, you can simply add one extra variable to the class of a type that can used for dynamic lookup(for example std::map) and make that variable private so no one except you in your class can access it:

class foo {
    std::map<std::string, boost::any> functionVariables;

public:
    void test1() {
        int visitNumber;
        auto i = functionVariables.find( "test1" );
        if( i == functionVariables.end() ) {
            // This is first visit of the function, initialize your variable
            functionVariables["test1"] = (visitNumber = 0);
        } else {
            // It is already initialized, use it
            visitNumber = ++ *boost::any_cast<int>(&*i);
        }
    }
};

Not the answer you're looking for? Browse other questions tagged or ask your own question.