google / clojure-turtle

A Clojure library that implements the Logo programming language in a Clojure context
Apache License 2.0
425 stars 41 forks source link

figure out using macros in ClojureScript #15

Closed echeran closed 8 years ago

echeran commented 8 years ago

The forms all in repeat, as they're using in clojure-turtle, are implemented as macros. Can they be used in a CLJS REPL, and if so, how?

Some CLJS macro blog posts from Mike Fikes: http://blog.fikesfarm.com/posts/2016-03-01-clojurescript-macro-sugar.html http://blog.fikesfarm.com/posts/2016-03-04-collapsing-macro-tower.html

jeisses commented 8 years ago

The macro's are usable on the CLJS REPL, they just have to be required specially:

(require '[clojure-turtle.core :as t :refer-macros [repeat all]])
(repeat 3 (all (t/forward 30) (t/left `90))

This should probably be mentioned in the readme.

1 difference is that CLJS macros must be defined in a different compilation stage then from where they are used. E.g. repeat and all can't be used inside clojure-turtle.core, but can be used from the REPLs cljs.user.

I just realize that in the examples we ns into clojure-turtle.core, probably because CLJS doesn't have use or :require :all and one doesn't want to prefix all commands.

If we want the macros available inside the core namespace they should be defined in their own ns, like clojure-turtle.macros. This could be for CLJS only, needing an extra import like (require-macros '[clojure-turtle.macros :refer [repeat all]])