Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Additional Clojure project source files

When lein sets up a project, a core.clj file is created along with other directories and files. I want to know if I can split core.clj's content off to another source file under

../myproj/src/myproj/

and if so, how to access that data from core.clj.

like image 428
octopusgrabbus Avatar asked Nov 07 '12 14:11

octopusgrabbus


3 Answers

IIRC (I don't have a project handy to check), everything in your src/myproj/ directory is in the 'myproj namespace. So your core.clj file will be in the namespace 'myproj.core. Other files will be in their own namespaces within the 'myproj namespace (e.g. 'myproj.other-file for other_file.clj), and can be pulled into core.clj by doing:

(use 'myproj.other-file)

or, in an ns declaration:

(ns myproj.core
  (:use [myproj.other-file]))
like image 64
paul Avatar answered Oct 20 '22 00:10

paul


You can split up definitions over as many files as you like, though it's idiomatic to put a single namespace in a single file and vice versa.

See http://clojure.org/libs for how to name and load files and namespaces. One thing to keep in mind is that dashes in namespaces translate to underscores in file names.

like image 26
Joost Diepenmaat Avatar answered Oct 20 '22 00:10

Joost Diepenmaat


Leiningen's project.clj is to define a project var that's nothing more than a map with keys denoting project parameters (it's an idiom in Clojure to use def[name] to create a var with the [name] name which usually is a map - the most basic yet very useful data structure).

See the defaults var in the source code of Leiningen 2 for the defaults.

With that said, before you call the defproject macro, you can do whatever you want in the project.clj - it's a Clojure script after all and your imagination (and familiarity with Clojure) is only what might constrain you. In fact, you may do whatever you want with the var after it's created. Think of project.clj as a Clojure application to manage your project.

As an example, before profiles were introduced in Leiningen 2, there was a "trick" to have a single var with common dependencies for :dependencies and :dev-dependencies attributes. Just to warn you again - it's no longer necessary in Leiningen 2 as it offers profile facility. Have a read of Testing your project against multiple versions of Clojure if you're curious how it was in the past.

like image 28
Jacek Laskowski Avatar answered Oct 19 '22 23:10

Jacek Laskowski