Ask HN: Is there a way to define classes at runtime in any language?
I was just thinking about chatbots, and the ability to make logical deductions. Like Men are Mortals, Socrates is a man, so Socrates is mortal. So Mortals could be the superclass, men could be the subclass, and Socrates could be an instance of the subclass men, and would be a member of superclass Mortals. But it would be nice to have a way of defining these classes at runtime, so in a chatbot if the user posed such a scenario to the chatbot, the chatbot could use some pattern matching, construct the classes on the fly, and figure out the relations, thereby sounding "intelligent". Can this be done? Classes made at runtime instead of compile time?
5 comments
[ 0.26 ms ] story [ 15.3 ms ] threadI think you may get further if you look at a language like Prolog -- work along the lines of predicates, rules, and attributes.
But if you really wanted to use the language's classes, there are languages that support it. Javascript is one. I think Ruby lets you do this too. Python lets you declare classes at runtime, but I don't think it gives you as much flexibility as the former two. Still, any of the three should be powerful enough to do what you want. Scheme will let you do it too.
You can also use "new.classobj(name, bases, dict)" -- not quite sure what the difference is.
http://www.jroller.com/obie/entry/deep_integration_of_ruby_a...