4

For example pattern matching is a programming language feature that can be added to the clojure language through macros: http://www.brool.com/index.php/pattern-matching-in-clojure

What other language features can be added to the language?

mudgen
  • 7,213
  • 11
  • 46
  • 46
  • 12
    "If you give someone Fortran, he has Fortran. If you give someone Lisp, he has any language he pleases." Guy Steele – Julien Chastang Aug 23 '11 at 04:22
  • 1
    It would be easier to list things you *can't* add to the language this way. Unfortunately, `goto` is among them. – SK-logic Aug 23 '11 at 10:59
  • 1
    The JVM imposes some limits such as lack of proper tail calls (http://blogs.oracle.com/jrose/entry/tail_calls_in_the_vm). – ponzao Aug 23 '11 at 12:30

3 Answers3

2

Off the top of my hat I have two examples, but I'm sure there are more.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • One might also note David Nolens work on [core.logic](http://github.com/clojure/core.logic), re mini-kanren. – kotarak Aug 23 '11 at 09:18
0

You can effectively add any language features you like.

This follows from the ability of macros to construct arbitrary code at compile time: as long as you can figure out what code you need to generate in order to implement your language features, it can be achieved with macros.

Some examples I've seen:

There are a few caveats:

  • If the feature isn't supported directly by the JVM (e.g. tail call optimisation in the mutually recursive case) then you'll have to emulate it. Not a big deal, but may have some performance impact.
  • If the feature requires a syntax not supported by the Clojure reader, you'll need to provide your own reader (since Clojure lacks an extensible reader at present). As a result, it's much easier if you stick to Clojure syntax/forms.
  • If you do anything too unusual / unidiomatic, it probably won't get picked up by others. There is a lot of value in sticking to standard Clojure conventions.
  • Beware of using macros where they are not needed. Often, just using normal functions (perhaps higher order functions) is sufficient to implement many new language features. The general rule is: "don't use macros unless you absolutely need to".
mikera
  • 105,238
  • 25
  • 256
  • 415
0

I think its a stupid question to ask what can be added, what you should ask is what you cant add. Macros allow you to hook into the compiler that mean you can do almost anything.

At the moment you cant add your own syntax to the language. Clojure does not have a user extenseble reader, this means you don't have any reader-macros (http://dorophone.blogspot.com/2008/03/common-lisp-reader-macros-simple.html). This is not because of a technical problem but more a decition by Rich Hickey (the Clojure creator).

What you can not do is implement features that need virtual maschine support like add tail call semantics or goto.

If you want to see some stuff that has been done: Are there any Clojure DSLs?

Note that this list is not 100% up to date.

Edit:

Since you seem you took pattern matching as an example (it is a really good example for the power of macros) you should really look at the match library. Its probebly the best fastest pattern matching library in Clojure. http://vimeo.com/27860102

Community
  • 1
  • 1
nickik
  • 5,809
  • 2
  • 29
  • 35
  • 1
    You still can add your own syntax: just replace the whole Clojure reader with your own, and use it as `(my-include "file.clj")` macro. – SK-logic Aug 23 '11 at 17:39