5 ms·
Metaprogramming: Ruby vs. Javascript
- pkulak 17y agoI had no idea that you could define a method on a single instance with access to the current scope. I used to always call instance_variable_set before def to get the data I needed in there.
- tumult 17y agoThere is no metaprogramming going on in the JavaScript here. Closures are not metaprogramming. It's just programming. I do not want to call names, but the author does not know what he is talking about. Metaprogramming inside of a running JS system would involve eval() and generating JS code from strings, which you don't want to do. It's prone to exploding in your face and the performance profile is terrible. Please read the article and think about its quality before voting.
- drewolson 17y agoI'm the author and I appreciate the insights. I wrote the article about what I consider to be building blocks of metaprogramming. I don't agree at all that eval() is the only piece of JS code that is metaprogramming. I believe that being able to dynamically define methods on objects at runtime is an example of "code that writes code" and hence metaprogrmming. YMMV.
- tumult 17y agoNo, that is simply programming with dynamic features. Nearly all modern programming tasks involve this. If you would like to look at some interesting JavaScript metaprogramming, check out projects like Objective-J/Cappuccino, Clamato, BiwaScheme, HotRuby, and a Python one I'm forgetting the name of (not Pyjamas, that does not run itself in JavaScript, it is compiled by a different language.) These pre-process some other types of code into native JavaScript structures which execute in the browser or JavaScript interpreter. They are distinct from inserting snippets of live generated code from strings into a system that is already live (as in eval() and friends) to modify it at runtime. That is one of the worst ways to use metaprogramming. What you want is a system that is self-hosting, in order to let you express yourself more deeply in the language. That way, your metaprogramming constructs execute in a controlled environment instead of chaotically determined by various factors at runtime. Lisp is the prime example of this.
- silentbicycle 17y agoIn his defense, I'd say that the line between "metaprogramming" and using the features a dynamic language gives you is fuzzy. I'm not sure if I consider changing the semantics of a data type in Lua through its metatable metaprogramming or not, for example. Generating source code at runtime definitely counts as metaprogramming, but doing that and having to re-parse it is probably the coarsest and most error-prone way. Lisp routes around this by working with ADTs directly, but that forces a lot of other trade-offs in the language design.
- deleted 17y ago[deleted]
- ynniv 17y agoWhat you're describing is a compiler, which is also not metaprogramming.
- tumult 17y agoIf a compiler is implemented in the language it compiles, and exposes compilation again to the program it compiles, then the implementation is metaprogrammable.
- ynniv 17y agoYour point? You said that someone should look to these systems as cases of "metaprogramming". Now you say that they are "metaprogrammable". You seem to have a very specific notion of metaprogramming, what exactly would you consider to be a prototypical use of it?
- ytinas 17y agoMetaprogramming doesn't require eval. Meta programming is being able to ask the program about itself and make changes to it during run time.
- tumult 17y agoNo, that is introspection (also known under names such as RTTI, object metadata, and some others). Metaprogramming is the act of programming your programming tools/program itself.
- ytinas 17y agoIntrospection is a form of meta programming (if you're making choices based on what you learn from introspection). When we say "meta x" we mean talking about x, e.g. "meta discussions" on HN is discussions about discussions. Meta programming is writing programs about the program, which can also just be doing introspection and making choices based on that.
- tumult 17y agoYou are mistaken. If normal programming constructs did not allow the programmer to choose behavior based on input, then every program would have a fixed execution path, would have the same output every time. and metaprogramming itself would not be possible. You are arguing that function arguments are metaprogramming, which is not what the use of the word 'metaprogramming' connotates. Metaprogramming, in the domain of computer programming, specifically carries the intention of programming in such a way that one or more sub-programs are created from the perspective of the one the programmer is working in.
- ytinas 17y ago>You are arguing that function arguments are metaprogramming No, I'm not. We seem to have a terminology mismatch somewhere. Asking a function how many arguments it takes and then taking some action based on that fact would be meta-programming, but taking input isn't.
- proemeth 17y agoThere is no such thing as metaprogramming. To help convince ourselves: http://gilesbowkett.blogspot.com/2009/07/do-you-believe-in-magic.html http://gilesbowkett.blogspot.com/2009/07/do-you-believe-in-m...
- deleted 17y ago[deleted]
- ytinas 17y agoI've had someone do that to me a time or two. I don't even have the power to down vote them back at this point! :)
- aaronblohowiak 17y agoPG does some detection of voting rings and nullifies the votes. He does this for upvotes, not sure about downvotes.
- ytinas 17y agoI have the feeling that some of mine have been corrected (maybe all where this was happening).
- deleted 17y ago[deleted]
- aaronblohowiak 17y agoYour definition of metaprogramming is not the accepted one. http://en.wikipedia.org/wiki/Metaprogramming http://en.wikipedia.org/wiki/Metaprogramming
- tumult 17y agoI just read it, and it is indeed what I have just said. Unless you would like to quote a specific part of the article, instead of simply pasting a link to a page on Wikipedia?
- ytinas 17y agoIn my next reply in our conversation I was going to quote exactly this article: "The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the object language. The ability of a programming language to be its own metalanguage is called reflection or reflexivity." Reflection is what I was describing. Asking a function its arity is indeed accomplished by passing said function to some other function, but if you want to find the source code for this function look for "reflection" because that's where it should be defined. "Metaprogramming usually works through one of two ways. The first way is to expose the internals of the run-time engine to the programming code through application programming interfaces (APIs). The second approach is dynamic execution of string expressions that contain programming commands. Thus, 'programs can write programs'. Although both approaches can be used in the same language, most languages tend to lean toward one or the other." You seem to only accept the second kind as meta-programming. What do you base your description on? Knowing that would make it easier to see where you're coming from and hopefully what the source of the misunderstanding is.
- tumult 17y agoIn general, yes, I would consider the second kind as more meta-ish. Even though I think literal strings are a terrible way to accomplish metaprogramming, a set of API calls for introspection is not what I would consider metaprogramming, since they will almost always be accomplished through runtime calls and do not actually generate any other code that can be further manipulated. Reflection as it is defined in that passage is more of what I am talking about, yes. But I think most languages lack this, including JavaScript. Some good examples are Lisp, Template Haskell, and the macro tools available in OCaml. I have nothing against runtime features, but I do not really consider it metaprogramming if that is the only way to accomplish it, since you are effectively being locked out of a true meta-circular system as a programmer. This style of programming is what defines Lisp and many other languages in the functional realm.
- ynniv 17y agoThere is no metaprogramming going on in the JavaScript here. Correct. The author compares Ruby to JavaScript in a situation which requires metaprogramming in Ruby, but not in JavaScript. Metaprogramming inside of a running JS system would involve eval() Incorrect. You could implement pre/post conditions in JavaScript by using metaprogramming, and it would not use eval(). The basic idea would be to iterate functions on MyClass.prototype, replacing each one with a new function that calls the original. This would be code (the pre/post implementation) modifying code (the MyClass definition). Please read the article and think about its quality before voting. Please calm down, the article is useful to Ruby people who are unfamiliar with JavaScript, and the literal definition of metaprogramming (code that modifies code) is largely misunderstood anyway. The most correctly you could ever use the term would be in reference to runtime or compile time optimization or safety checking, instrumented profiling, or logging, and I rarely ever see it used that way.
- Vitaly 17y agoHe is adding methods to a class once it already have some instances and those instances get the new functionality. That's 'monkeypatching', which IS a form of metaprogramming.
- iamwil 17y agoOne difference I found is that Javascript has no "method_missing" call. Firefox is ahead of the pack here, as it implements a non-standard __noSuchMethod__ call, but that doesn't exist in the latest chrome or safari. In addition, there's no way (that I've found) to override the subscript operator [] in javascript.
- aaronblohowiak 17y agoLooks like you'd rather be programming in Lua.
- iamwil 17y agoOr Ruby. I've does Lua before, and found it pretty easy, though I hadn't done too much metaprogramming there. But from what I saw, its minimalism is like javascript, and its metatables like ruby's metaclasses and javascript's prototype inheritance.
- silentbicycle 17y agoI think that Lua is what Javascript may have grown into if it had 15 years mature, rather then getting its design prematurely frozen. They seem to have very similar design goals, though where Javascript has the web, Lua has C. Lua is really powerful, but it's been carefully designed so that a subset of the language can be used for configuration files, data dumps (much like JSON), etc., without having to know anything about programming. The deep elements are there, you just have to look for them. Coroutines, prototypes, metatables, tail-calls, first-class functions, eval, real lambdas, etc. And, did I mention that it mixes well with C?
- iamwil 17y agoYeah, I recently saw that it had coroutines, which was a surprise to me. coroutines can be used to implement fibers and is also related to continuations. I think it's deceptively simple, so programmers might not think there's much there. In addition, it's pretty minimalistic, you don't get OO out of the box (which isn't necessarily a bad thing, depending on what you're doing), and the number of libraries pales in comparison, all of which deter others for exploring it more fully. That only means that the field is ripe for some exploration.
- bonquesha99 17y agoThis project is pretty awesome: http://jsclass.jcoglan.com/ http://jsclass.jcoglan.com/ - Ruby-style JavaScript
- swannodette 17y agoWow, who would have thought this article would have started such a contentious thread. An interesting point is that more powerful languages blur the distinction between programming and meta-programming, weaker languages make the distinction more clear.
- silentbicycle 17y agoPerhaps it would be more accurate to say that "metaprogramming" techniques have become more integrated into conventional programming in the language?