Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda function defined with defn? #121

Open
katawful opened this issue Jun 5, 2022 · 2 comments
Open

Lambda function defined with defn? #121

katawful opened this issue Jun 5, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@katawful
Copy link

katawful commented Jun 5, 2022

In Clojure, a function defined with defn can take multiple argument lists to achieve multi-arity. In Fennel, this achieved with simply passing nil to unneeded args. If an arg is strictly needed, lambda is used over fn:

(defn func 
  ([arg1] (print arg1))
  ([arg1 arg2 arg3] (print arg1 arg2 arg3)))
(func 1) ; --> prints '1'
(func 1 2 3) ; --> prints '1 2 3'
(fn func [arg1 arg2 arg3] (print arg1 arg2 arg3))
(func 1) ; --> prints '1 nil nil'
(lambda func [arg1 arg2 arg3?] (print arg1 arg2 arg3?))
(func 1) ; --> Missing argument arg3? on unknown:4

Different ways to achieve the same end goal, mostly because Fennel is based on Lua which has its own limitations compared to Java

In Aniseed however, defn only compiles to the named function desired defined with fn, which is then added to the current module. Example:

(defn func [arg1 arg2 arg3?] (print arg1 arg2 arg3?))
(func 1) ; --> falsely prints '1 nil nil' instead of a runtime error
; expansion
["ANISEED_DELETE_ME"
 (fn func [arg1 arg2 arg3?] (print arg1 arg2 arg3?))
 (tset *module* "test" test)]

arg3? cannot be nil checked and return a runtime error as defn always compiles to fn. This seems incongruent with what this macro is attempting to mimic

@katawful katawful changed the title Lambda checked defn function? Lambda function defined with defn? Jun 5, 2022
@Olical Olical added the enhancement New feature or request label Aug 7, 2022
@Olical
Copy link
Owner

Olical commented Aug 7, 2022

Hmm, this would be a breaking change for all users I think, anyone who allowed nils or optional args would suddenly have this code break on update.

If I ignored backwards compatibility I guess defn would need to wrap your functions you provide to switch to the one with the correct arity AND use lambda instead for arity checking. Maybe this could be some sort of option you can switch off / have to opt into.

Maybe instead I could define defns/defn!/defun/deflambda that has this new, more locked down, but multi arity supporting idea.

@katawful
Copy link
Author

katawful commented Aug 7, 2022

If it would lead to breakages then a new macro would be far more ideal yeah. deflambda seems like the most reasonable name to me, as defn already contains fn from fennel's keywords

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants