(Quick Reference)

5 Function Namespaces

Version: 2.0.0.RC4

5 Function Namespaces

Clojure allows functions to be defined in a namespace which allows multiple functions with the same name to be defined, provided they are in different namespaces.

5.1 Declaring Function Namespace

When defining Clojure functions you need to specify a namespace.

;; src/clj/general.clj

;; Functions defined in this source file will be in the grails namespace (ns grails)

;; function declarions go here ;; ...

;; src/clj/reports.clj

;; Functions defined in this source file will be in the accounting namespace (ns accounting)

;; function declarions go here ;; ...

5.2 Specifying A Namespace At Function Invocation Time

By default the plugin will invoke clojure functions which are defined in the grails namespace. In order to invoke functions in any other namespace, the invocation needs to specify the name of the target namespace using Groovy's subscript operator.

// grails-app/controllers/demo/SomeController.groovy

package demo

class SomeController {

def add(int x, int y) { // this will invoke the report function in the grails namespace clj.report()

// this is equivalent to the code above clj['grails'].report()

// this will invoke the report function in the accounting namespace clj['accounting'].report()

// … } }