Weakly/Dynamically typed languages confuse me

Pages: 1... 345
That's a trap. If a language is capable of doing it without some hackery
What exactly second lambda do? Your words about trap makes me uneasy. If it does som obscure thing that cannot be seen from code by people who do not know language well, you should explait what exactly code does.
I'm changing the class method at runtime. I guess the example would be more interesting if the new method was expecting different types of arguments.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <functional>

class Foo
{
  public:
    std::function<int(int)> doit = [](int val){return ++val;};
};

int main() {
    Foo foo;
    std::cout << foo.doit(10) << '\n';
    foo.doit = [](int val){return val*2;};
    std::cout << foo.doit(10);
}
11
20
http://ideone.com/9m6r1Y

Or you can use boost::any (*doit)(...); for different types and amount of input and output.
However I do not see usefulness of changing method to drastically different one in runtime: it leads to strange and unexpected behavior. For example what if code which expects previous version of method tries to call new?
Last edited on
Back to the example with extension language - it’s true that extending C++ with Lua is equivalent to using Python for both the application and extension language, it’s just that the latter so much easier. Not in theory, but in reality.
It still has nothing to do with type systems, though, or with the abstractive power of a language for that matter.
Ok, true - it is not that easily possible to change a method to a completely different one. Or to redefine a property to a property of a different type. I'm really curious, when can it be useful?
The example of changing the number of arguments a function takes is a bad one - you can do it, but I think you should never do it.
Topic archived. No new replies allowed.
Pages: 1... 345