5 ms·
I never said it was pretty, in fact I said quite explicitly that it wasn't as pretty as Ruby or Python, but it doesn't take good looks to work well. Java and C
by nuclear_eclipse 15y ago
I never said it was pretty, in fact I said quite explicitly that it wasn't as pretty as Ruby or Python, but it doesn't take good looks to work well. Java and C# are even more verbose than PHP but they can also do reflection just as well as Ruby and PHP can.
- bad_user 15y agoJava and C# are even more verbose than PHP but they can also do reflection just as well as Ruby and PHP can. Err, no, they cannot -- in Java/C# you cannot create or redefine functions / methods / entire classes at runtime. To do that, you need to get into bytecode generation, and that's not a feature of the language anymore; not to mention problems with the fact that their respective VMs weren't designed to allow the regeneration of methods/classes at runtime, so you run into problems like the permanent-generation in the GC (i.e. classes aren't garbage collected, so if not careful you may end up with a huge memory leak). "more verbose" doesn't really describe the shit you must do to make available the same tricks you can pull in Python/Ruby. Comparing this to C + assembly generated at runtime would be stretching it, but just a little -- having the ability to generate assembly doesn't mean that C has meta-programming facilities ;)
- damncabbage 15y agoIn all fairness, you're restricted as to how much you can do that PHP as well: The best you can do is modify the class definition in the code itself, and declare a __call() or __get()/__set() functions that park themselves in front of method calls and variable accesses. You can't go re-opening classes or directly adding methods or variables. (To make matters worse, __call() and friends are also more than a little slow as well.)
- nuclear_eclipse 15y agoRedefining or extending methods or classes at runtime isn't reflection; that's monkeypatching and/or rebinding new definitions to existing names. I would agree that Java, C#, and PHP lack the ability to do this without a lot of overcomplications, but at the same time, I've never really found myself in a situation where rebinding or extending classes at runtime was really the best solution. I would actually be quite interested in seeing a usage of that sort of magic that actually makes the solution easier and simpler than other options.
- bad_user 15y ago@nuclear_eclipse, look up this thread and you'll see that the discussion started from a mention of meta-programming. Also, IMHO, reflection (per se) is basically useless in a dynamically-typed language, unless you also have the ability to redefine stuff. I've never really found myself in a situation where rebinding or extending classes at runtime was really the best solution. That's only because you aren't used to it, and you're dismissing it based only on a limited experience with what is possible; although the libraries you're using may be doing it without you even noticing it. If you're a Java developer, what do you think frameworks like Spring, Hibernate, Guice are doing? And why in the world would you be using dynamic-typing if everything was set in stone from compile-time? Structural-typing (like in Scala) works a lot better ; although I can see why there's a misconception about what dynamic-typing really enables. That's the thing with half-baked features -- of course they are hard to use and error prone when the language itself isn't really built around it. But in a language like Ruby, where the "class" block's main purpose is to open a scope where "self" (the implicit) parameter points to that class, and inside the definition you can put anything, like real code that does stuff, and where creating new classes (in case they don't exist) is just a side-effect -- modifying classes and objects at runtime is everything you're doing (whether you like it or not). But it does give you the tools to deal with it. Same thing happens with Python, but more with functions and class methods, rather than classes, because that's what Python was optimized for -- decorating functions with extra behavior, since functions are first-class citizens. In Python it doesn't work so well for classes, since inside the definition itself you don't have access to "self", but you can also patch classes with great effect, just not with so much syntactic-sugar as in Ruby -- that's why people shy away from it and choose to deal with functions/methods instead, although there is some cool meta-programming stuff done with classes in Django. That's what decorators do btw (which look similar to Java annotations, but have a lot more bang for the buck :)) -- http://www.python.org/dev/peps/pep-0318/ http://www.python.org/dev/peps/pep-0318/ I would actually be quite interested in seeing a usage of that sort of magic http://rubyonrails.org/ http://rubyonrails.org/ -- and really, don't be fooled by all the clones available. I worked with Symfony and CakePHP -- every Rails-clone sucks compared to Rails. You have to try it for real in order to see what it does differently. If you're a fan of Java: http://aspectwerkz.codehaus.org/ http://aspectwerkz.codehaus.org/ See that link above? -- that's so easy to do in Python/Ruby that it doesn't deserve a library ;-) This is a good read on Ruby's meta-programming facilities, while not being so hard to read btw: http://pragprog.com/titles/ppmetr/metaprogramming-ruby http://pragprog.com/titles/ppmetr/metaprogramming-ruby Also, terms like "monkey-patching" and "magic" are derogatory terms for good engineering practices people have been using since the 70-ties when generating assembly at runtime was the only reasonable way to make complicated algorithms to work, while maintaining some sanity, like those in the field of artificial-intelligence. In the meantime people got so obsessed with preventing employees from shooting themselves in the foot, that aberrations like C++ and Java happened. Unfortunately for everybody, bad developers write bad code regardless of the programming language.