11 ms·
Why don't I metaprogram more?
- endlessvoid94 12y agoIt's hard to imagine what real lisp-like macros look like with other, non-homoiconic langauges. Anyone have an example?
- daeken 12y agoNemerle is non-homoiconic, but has full-on macros. Here you can see some examples from my Nextem library (a bunch of niceties I built up over the years): http://trac.assembla.com/nextem/browser/trunk/Examples/String.n http://trac.assembla.com/nextem/browser/trunk/Examples/Strin... along with the macros behind it http://trac.assembla.com/nextem/browser/trunk/Macros/StringFormatting.n http://trac.assembla.com/nextem/browser/trunk/Macros/StringF... http://trac.assembla.com/nextem/browser/trunk/Macros/Regex.n http://trac.assembla.com/nextem/browser/trunk/Macros/Regex.n The macros run at compile-time, and emit code by use of the <[ ]> blocks -- those get turned into ASTs at compile-time and get filled with the proper data.
- Cyther606 12y agoNimrod has strong metaprogramming capabilities. template htmlTag(tag: expr) {.immediate.} = proc tag(): string = "<" & astToStr(tag) & ">" htmlTag(br) htmlTag(html) echo br() produces: <br> http://nimrod-lang.org/talk01/slides.html http://nimrod-lang.org/talk01/slides.html https://news.ycombinator.com/item?id=7071429 https://news.ycombinator.com/item?id=7071429
- S4M 12y agoJulia is not lisp like, and has macros [1]. The way it works is that your code is first interpreted to get the AST that looks like LISP code (`a+bc+1` becomes `+(a,(b,c),1)`) and your macro operates on that. So basically it works like a lisp macro (until you want to create a macro that creates macro, which is something I haven't done but heard about). http://docs.julialang.org/en/release-0.2/manual/metaprogramming/ http://docs.julialang.org/en/release-0.2/manual/metaprogramm...
- S4M 12y agoCORRECTION: `+(a,*(b,c),1)` is NOT like LISP code. However, Julia helps you to get the head of an expression (in this case, +) and it's args.
- MartinMond 12y agoCheck out Elixir's metaprogramming guide: http://elixir-lang.org/getting_started/meta/1.html http://elixir-lang.org/getting_started/meta/1.html
- jpd 12y agoBesides those already mentioned, there's Dylan, which was originally S-expression based before switching to it's Wirth-style syntax.
- tel 12y agoHaskell's "Template Haskell" system is pretty sophisticated... And challenging enough to be some deep black magic. You end up being able to reify the entire Haskell syntax tree from "quoted" fragments, manipulate and generate a modified tree, and then "print" it back into program flow. Totally capable, but not terrifically fun. It also has some compilation restrictions which are annoying if necessary.
- dllthomas 12y agoI just (last night) dug into some TH hackery. I put together a quasiquoter for testing/debugging that would walk a do block and decorate every statement with (`onException` ...) set up to print the source location when an exception is thrown. I actually had to first parse with haskell-src-exts to get the location info, and currently I'm turning it into an ExpQ by pretty-printing and reparsing - which I don't love, but gets the job done. https://github.com/dlthomas/snowdrift/compare/5094b1ca0e5002c0d12a7f714b083d88d450ddb3...e7bf8ac8b6320fcc46233248170920fc97d805c9 https://github.com/dlthomas/snowdrift/compare/5094b1ca0e5002...
- judk 12y agoTH also ruins Haskell by inserting inivisble code with spooky action at a distance, where you have to pre-import symbols that TH will insert later, and compiler errors refer to code defined somewhere else but print line numbers of where the generated code is inserted. TH needs a compiler!
- danschuller 12y agoMetalua is a good example of a macros with an Algol-like syntax. It's built on top of Lua - as you might guess from the name. http://metalua.luaforge.net/quicktour.html http://metalua.luaforge.net/quicktour.html
- klibertp 12y agoSweet.js (http://sweetjs.org http://sweetjs.org) and MacroPy (https://github.com/lihaoyi/macropy https://github.com/lihaoyi/macropy) come to mind. Actually "real macros" are just macros which operate on AST instead of on strings, they are hard to do if you have to re-implement language parser, but rather easy if the language exposes its AST api, like in Python case.
- pconner 12y agoThe title is misleading. By itself, it seems to imply that the author has something against the practice of metaprogramming in general, rather than just Ruby metaprogramming.
- infraruby 12y agoYou can do metaprogramming in Ruby without using the reflection features of the language: http://infraruby.com/blog/ruby-metaprogramming-with-s-expressions http://infraruby.com/blog/ruby-metaprogramming-with-s-expres... http://infraruby.com/blog/the-structure-of-s-expressions http://infraruby.com/blog/the-structure-of-s-expressions http://infraruby.com/blog/generating-an-html-generator http://infraruby.com/blog/generating-an-html-generator
- teh_klev 12y agoUnless I'm building a framework, should I really care? I'm a "mort" line of business apps dev (well not just that, I do lots of other things, but hey). Do I really need to care about metaprogramming, my gut instinct says no because our business and customer requirements change all the time, and unpredictably. And if I was spending time thinking about "meta programming", nothing would get done.
- JimBarrows 12y agoI would think of it as more code that generates code, or functions that get called at compile time rather than run time. I've had code that I copied and pasted, and changed, but couldn't factor out into a separate function easily. In those cases, a macro to write the code would have worked nicely.
- jfabre 12y agoNo offense, but I think your comment is a bit short-sighted. Meta-programming is just another tool in your tool-belt. It's not going to take you 6 months of practice to "get it".
- bulte-rs 12y agoI don't disagree with you, but some people might actually have problems 'understanding' metaprogramming; the same way some people have difficulties understanding pointers, or working through TAoCP/SICP/HTDP. Problem is, once you get it; it's unfathomable that you might not understand it.
- fred_durst 12y agoI imagine if you spent most of your time worrying about language design in general, your "business apps dev" wouldn't be too successful. Some people look a little further than the meal on their own plate.
- MichaelGG 12y agoIt's my experience, even in boring business code, that learning more techniques can significantly help. Common OO/procedural code has a ton of boilerplate that can be eliminated with functional and metaprogramming techniques, for instance. But you're right, you don't need to learn anything. Facebook is doing well and it was written in PHP. I hear many popular books were written on typewriters, or even with pen and paper.
- norseboar 12y agoWhile the link focuses on Ruby, I think there's a more general reason (which also explains why more languages don't support a lisp-like macro): cooperative development. When you're writing code in a vacuum, you can fill it with all the metaprogramming you want. You probably have some preferred style, and your metaprogramming will reflect that. In something like lisp, this gets taken to the extreme -- code can look very different from what traditional lisp looks like when there are macros involved. While this is great for developing solo, or with a small group, it becomes too much to handle as more people onboard. Every new contributor needs to learn your particular style, and how your macros work, and then how to apply them effectively. Its much easier when new functionality and expressiveness is added through a common format (like adding a new method). As a lisp fan, those methods feel clumsy to me. But if you want to do something that's so big you need more contributors, it's worth the tradeoff.
- MichaelGG 12y agoDoes anyone have some really good examples of metaprogramming? I bought a book on Ruby Metaprogramming and the examples seemed to be all about disguising a string parameter as a member name. So for instance: foo.somefield Versus: foo["somefield"] And variations on that theme. While it's cute, I guess (you're not type checking anyways so nothing lost, I suppose), I don't really get it.
- jbackus 12y agoDynamic finders in Rails 3.1's ActiveRecord: http://git.io/hdb0QA http://git.io/hdb0QA
- MichaelGG 12y agoI've seen that before, but isn't that the same theme? db.find_by_userid(123) versus user.find_by("userid", 123)? If everything is checked dynamically, does it really make a difference beyond the 3 characters? Sure find_by_userid looks somewhat nicer, for sure. But is that really it? Any examples that aren't trivially replaced by a string param (or two)?
- stevepike 12y agoThe CanCan ruby authorization library does a neat bit of metaprogramming. It offers a function "load_resource" which you put at the top of your controller. The function uses the name of the controller class and the name of the currently executing controller action to automatically set a resource. It looks like this: class CarsController < ActionController::Base load_resource def index # @cars is defined end def show # @car is defined end end
- beemoe 12y agoI don't know what qualifies as "really good examples" but there are lots of random things that use metaprogramming in some capacity and are popular. Lots of IDLs, serialization formats, ORMS etc. I am thinking stuff like Protocol Buffers, Hibernate, Swagger, etc.
- 12y ago
- CmonDev 12y agoDoes it really matter in a dynamic scripting language? When using reflection in a strong programming language there is a chance it will fail at runtime instead of being checked at compile time. Ruby only fails at runtime, so as long as the code is shorter it should not be a much bigger problem than it already is.