Fork me on GitHub
#babashka
<
2024-03-13
>
escherize04:03:22

What are my options for translating this sort of thing into babashka?:

(deftype MyString [^String s]
  Object
  (^String toString [_this] s))
I see that I can’t do it directly because adding methods on a SciType is not allowed. I’m trying to find a way to differentiate between strings and a different sort of string.

3
escherize04:03:05

The error looks like:

clojure.lang.ExceptionInfo: Method toString on class sci.impl.deftype.SciType not allowed!
{:type :sci/error, :line nil, :column nil, :file "<expr>"}
 at sci.impl.utils$throw_error_with_location.invokeStatic (utils.cljc:43)

escherize04:03:22

I don’t think I can use deftype, but maybe there’s another way…

dpsutton04:03:14

interesting

user=> (deftype MyString [s] Object (toString [_] (str "special " s)))
user.MyString
user=> (MyString. "foo")
#object[user.MyString 0x688bd4b7 "special foo"]
user=> (str *1)
"special foo"

borkdude08:03:05

indeed, you probably have a very old bb version

escherize02:03:09

For the sake of completeness, I figured out it was from calling .toString on the deftype. str works though, so issue’s solved

👍 1
Omer ZAK10:03:44

Seems that require and if do not play nicely together, at least when the playground is a Babashka script. The only difference among bb1.clj and bb2.clj is that bb2.clj executes its requires inside an if form. The result is that bb1.clj correctly executes without problems, and bb2.clj aborts with a clojure.lang.ExceptionInfo with Message: Could not resolve symbol: fancy/print-table. Why? Is it intentional? (Note: I do not have a real need to require inside an if. I stumbled by accident into this behavior, and would like to understand it.)

borkdude10:03:45

This is known as the gilardi scenario: https://technomancy.us/143

🙏 2
borkdude10:03:02

aliases you introduce are only to be used in the next top level form

Omer ZAK10:03:51

Thanks! This scenario serves to teach us that incidental complexity cannot be totally avoided. It can only be pushed away a bit.

borkdude10:03:05

you can use (requiring-resolve 'foo/bar) instead

1
mmer10:03:34

Hi, is there a prefered library for Base64 encoding in BB?

borkdude11:03:49

there is a library, https://github.com/clj-commons/digest, but you can also just use the Java API for this directly