Fork me on GitHub
#clojurescript
<
2019-11-17
>
lxsameer15:11:38

hey folks, i was wondering, shouldn't the artimastic functions like + and - behave like clojure's implementation rather than javascript ?

dpsutton16:11:13

What divergence are you referring to here?

lxsameer16:11:52

for example in clojure (+ "234" 234) leads to an exception while in javascript and clojurescript it returns to "234234"

p-himik16:11:36

AFAIK, the contract of such functions is that they always accept a numeric argument. Any other argument leads to UB.

p-himik16:11:38

Undefined Behavior.

lxsameer16:11:13

well, that's what i mean, I expect clojurescipt to be more like clojure rather than JS

lxsameer16:11:33

clojure has better defined artimastic functions

darwin16:11:06

it is not realistic to implement cljs with exact clojure arithmetic semantic, you would have to emulate it and that would be impractical

darwin16:11:38

another example would be regular expressions, they are not exactly the same between clojure and cljs, both use platform’s regex engines

darwin16:11:29

you would have to implement JVM regex engine on top of javascript as a library, which seems to be a poor idea to me 🙂

p-himik16:11:36

@U28TJ0DDZ It's UB for both CLJ and CLJS - they're the same in this regard. Just like NULL != NULL, UB != UB.

☝️ 1
lxsameer17:11:42

@U08E3BBST while i see your point but i think at least for artimastic fns it might worth doing that becasue at the end of the day one of the reasons to pick cljs over js is to avoid js weird behaviors

lxsameer17:11:37

@U2FRKM4TW it's not UB for clj, you'll get an exceptions that prevents any further assumption

p-himik17:11:12

UB does not mean "anything but exceptions". UB means that the contract doesn't specify the behavior. The docstring of + specifies that only numbers can be used as arguments. It means that using a non-numeric argument is UB.

1
lilactown16:11:39

I think the compiler/analyzer should warn you if you write that in a .cljs file, but adding the check to throw the exception would probably impact performance in what could be quite a hot path (arithmetic)

lilactown16:11:39

I believe the exception on the JVM comes from platform behavior, same as JS behavior of concating as a string

lilactown18:11:40

I know this sounds dumb, and I'm 99% sure it's not possible. but could one intern a macro in a purely CLJS namespace?

lilactown18:11:16

I essentially want to have a macro that defines other macros, that could be used within a .cljs file