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

Destructuring first argument of record's method does not work as expected (inconsistent with clojure 1.10.1) #512

Closed
charignon opened this issue Jan 25, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@charignon
Copy link

First and foremost, thank you so much @borkdude for all of your work, you brought the joy of clojure to scripting with your projects!

This is a bug report when trying to run some code under Babashka; the code worked with Clojure 1.10.1 but failed with Babashka.

I am using Babashka v0.2.7 , Linux Binary Distribution, which maps to version 0.2.2-SNAPSHOT of sci, hash f724f0e

Repro steps comparing the same code with clojure and with babashka:

Minimal code to repro the issue:

(defprotocol Foo (sayhello [_ name] "print a name"))
(defrecord Greeter [state] Foo (sayhello [{:keys [state]} name] (println state name)))
(sayhello (Greeter. "test") "john")

Clojure 1.10.1

$ clojure
Clojure 1.10.1:
user=> (defprotocol Foo (sayhello [_ name] "print a name"))
Foo
user=> (defrecord Greeter [state] Foo (sayhello [{:keys [state]} name] (println state name)))
user.Greeter
user=> (sayhello (Greeter. "test") "john")
test john
nil

Babashka v0.2.7:

$ bb
Babashka v0.2.7 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (defprotocol Foo (sayhello [_ name] "print a name"))
{:ns #object[sci.impl.vars.SciNamespace 0x57cba1e5 "user"], :methods #{#object[clojure.lang.MultiFn 0x1f6925bf "clojure.lang.MultiFn@1f6925bf"]}}
user=> (defrecord Greeter [state] Foo (sayhello [{:keys [state]} name] (println state name)))
#object[clojure.lang.MultiFn 0x1f6925bf "clojure.lang.MultiFn@1f6925bf"]
user=> (sayhello (Greeter. "test") "john")
nil john
nil

expected behavior: I would expect Babashka to print "test john" and not "nil john".

There is a workaround and records are not broken (see defrecord tests in this repo), but it may trip someone who is trying to convert code from clojure to babashka/sci.

@borkdude borkdude added the bug Something isn't working label Jan 25, 2021
@borkdude
Copy link
Collaborator

Thanks, I'll look into it!

@borkdude
Copy link
Collaborator

The bug is that destructuring isn't accounted for in the defrecord implementation, but this works:

(defrecord Greeter
    [state] Foo
    (sayhello [rec name]
      (let [{:keys [state]} rec]
        (println state name))))

@borkdude
Copy link
Collaborator

Fixed with 735527b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants