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

Reader parsing of #() combined with #_ behaves differently than in Clojure and ClojureScript #702

Closed
didibus opened this issue Jan 5, 2021 · 3 comments
Projects

Comments

@didibus
Copy link
Contributor

didibus commented Jan 5, 2021

version

Babashka v0.2.6 REPL.

platform

OpenSuse Linux running inside Windows WSL2

problem

On Clojure and ClojureScript, the following code:

(mapv #(rand-int 10 #_%) (range 100))

Returns:

[2 5 2 4 2 2 2 6 7 8 5 9 5 8 2 0 7 8 6 1 0 8 7 6 0 0 8 6 4 9 1 0 6 9 7 2 9 3 3 0 8 3 2 1 5 6 9 9 9 6 2 7 6 1 7 3 2 5 1 5 5 2 8 2 5 4 9 1 7 5 0 8 0 6 0 4 8 0 9 7 4 8 7 2 9 9 9 8 0 3 3 1 3 6 9 7 1 6 4 9]

But in Babashka it errors with:

clojure.lang.ArityException: Wrong number of args (1) passed to: mapv [at <repl>:1:1]

repro

At the command line:

> babashka
user=> (mapv #(rand-int 10 #_%) (range 100))

expected behavior

I expect it to work just like in Clojure and ClojureScript. Also to remark:

(mapv #(rand-int 10 #_%1 #_%2) (range 100) (range 100))

Also works in Clojure and ClojureScript.

@borkdude
Copy link
Collaborator

borkdude commented Jan 5, 2021

According to @puredanger this is undefined behavior:

https://twitter.com/puredanger/status/1280854707980972036

Having said that, I will take a look, if it's not an invasive change, then might fix.

Screenshot 2021-01-05 at 09 10 18

@borkdude
Copy link
Collaborator

borkdude commented Jan 5, 2021

From slack:

7m
alexmiller  I do not believe this is behavior you should rely on. We’ve even looked at changes to the discard reader recently re tagged literals or reader conditionals and it’s not clear to me that things would still work this way after those changes. So, please don’t do this.

6m
alexmiller  It’s so much clearer to just use fn

Screenshot 2021-01-05 at 14 10 27

So I won't explicitly support this, but I did find an issue in the parser that allows nested anonymous function literals:

$ bb -e '(#(+ % (#(+ % 1) 2)) 10)'
13

This probably violates what people expect, namely to throw with a warning that nested fn-literals aren't supported.

@didibus
Copy link
Contributor Author

didibus commented Jan 6, 2021

Like we discussed on Slack, I think this is actually more intuitive behavior, and its too bad Clojure/Script actually allow this, but it does seem to be accidental, and the official stance is not to rely on this behavior, as it is considered undefined. So feel free to close this issue whenever you want.

@borkdude borkdude added this to To do in Babashka Jan 10, 2021
@borkdude borkdude closed this as completed May 3, 2021
@borkdude borkdude moved this from To do to Done in Babashka Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants