rm-hull / infix

A Clojure library for expressing LISP expressions as infix rather than prefix notation
https://www.destructuring-bind.org/infix/
MIT License
107 stars 12 forks source link

How to call parameter-less functions? #14

Closed emnh closed 8 years ago

emnh commented 8 years ago

The following fails:

(defn
  circle-rectangle-intersects?
  [circle rect-x1 rect-x2 rect-y1 rect-y2]
  (let
    [circle-x (:x circle)
     circle-y (:y circle)
     circle-r (:r circle)
     rect-width (infix rect-x2 - rect-x1)
     rect-height (infix rect-y2 - rect-y1)
     circle-distance-x (infix abs(circle-x - rect-x1))
     circle-distance-y (infix abs(circle-y - rect-y1))
     check1 #(infix circle-distance-x > (rect-width / 2 + circle-r))
     check2 #(infix circle-distance-y > (rect-height / 2 + circle-r))
     check3 #(infix circle-distance-x <= (rect-width / 2))
     check4 #(infix circle-distance-y <= (rect-width / 2))
     corner-distance-sq #(infix (circle-distance-x - rect.width / 2) ** 2 + (circle-distance-y - rect-height / 2) ** 2)
     check5 #(infix corner-distance-sq() <= circle-r ** 2)
     ]
    (if
     (infix check1() || check2())
     false
     (if
       (infix check3() || check4())
       true
       (check5)))))
Compile Warning
Wrong number of args (1) passed to corner-distance-sq at line 274 /home/emh/github/rts/src.client/game/client/selection.cljs
Wrong number of args (1) passed to check1 at line 277 /home/emh/github/rts/src.client/game/client/selection.cljs
Wrong number of args (1) passed to check1 at line 277 /home/emh/github/rts/src.client/game/client/selection.cljs
Wrong number of args (1) passed to check2 at line 277 /home/emh/github/rts/src.client/game/client/selection.cljs
Wrong number of args (1) passed to check3 at line 280 /home/emh/github/rts/src.client/game/client/selection.cljs
Wrong number of args (1) passed to check3 at line 280 /home/emh/github/rts/src.client/game/client/selection.cljs
Wrong number of args (1) passed to check4 at line 280 /home/emh/github/rts/src.client/game/client/selection.cljs
rm-hull commented 8 years ago

So I think your issue can be distilled down into:

(defn f []
  (infix 3 + 7))

(macroexpand-1  '(infix f() + 10))
; => (+ (f nil) 10) 

And obviously trying to call (f nil) fails and gives you the arity warning in your clojurescript.

The offending line of code that generates this is (I think) https://github.com/rm-hull/infix/blob/master/src/infix/core.clj#L124-L126 - it always expects there to be at least one argument.

I do remember thinking this over previously: "why would you have a function with no-args?" - I guess possible answers are:

I think a fix would be fairly straightforward - watch this space.

rm-hull commented 8 years ago

Hi, there's a snapshot version on clojars (https://clojars.org/rm-hull/infix/versions/0.2.7-SNAPSHOT) with this fix in, if you could test it out and let me know if it works for you. I added some specific tests, so pretty confident it all works in clojure, but if you could check it out in clojurescript that would be great... after that I'll do a proper (non-snapshot) release.

emnh commented 8 years ago

Yup that works! Great! Thanks!

rm-hull commented 8 years ago

great stuff. 0.2.7 released