Arc Forumnew | comments | leaders | submitlogin
2 points by Pauan 5032 days ago | link | parent

About using + for only addition: I mostly agree, with the caveat that I think there should be a "join stuff together with implicit coercion" function. Maybe we could use `join` for that. But I wouldn't expect `join` to do addition on numbers... I guess it's a question as to whether we want Arc to lean more toward strong or weak typing.

For reference, JS is (very) weakly typed, and Python is (more or less) strongly typed. Arc (mostly) seems to follow JS (weakly typed).

---

In Python:

  5 + 10         -> 15
  "foo" + "bar"  -> "foobar"
  "foo" + 5      -> error
  5 + "foo"      -> error
  str(5) + "foo" -> "5foo"
In Arc:

  (+ 5 10)         -> 15
  (+ "foo" "bar")  -> "foobar"
  (+ "foo" 5)      -> "foo5"
  (+ 5 "foo")      -> error
  (string 5 "foo") -> "5foo"
In JS:

  5 + 10        -> 15
  "foo" + "bar" -> "foobar"
  "foo" + 5     -> "foo5"
  5 + "foo"     -> "5foo"
---

So... here are our options:

1) + works on numbers. Period. Requires explicit coercion if the arguments are not numbers. Provide something else for concatenation (join?)

2) + works on numbers. Period. Implicitly coerces arguments to numbers, and errors if it can't. Provide something else for concatenation (join?)

3) + works on everything, but if you want to add things of different types (like numbers and strings) you need to explicitly coerce. This is Python's behavior.

4) + works on everything, and the arguments are implicitely coerced according to some (hopefully sane) rules. This is what Arc and JS do right now.

---

By the way, the difference between Arc and JS is that Arc always returns the type of the first argument, and errors if it can't. Thus, when doing (+ "foo" 5) it coerces 5 into a string, and then concatenates. But, when doing (+ 5 "foo") it tries to coerce "foo" into a number, which obviously fails.

In JS, + is binary, and coercion rules take both operands into account, which is why the last example worked. This strategy would be possible in Arc, but I'm not sure how confusing/desirable that would be.

It's possible to do it in Arc too, if you're explicit:

  (+ "" 5 "foo") -> "5foo"
Though I think using `string` would be clearer, in that case.

---

P.S. I see strong/weak as not the same as static/dynamic. My understanding of it is that strong typing won't implicitly coerce: you have to be explicit. On the other hand, weak typing will try to guess what you meant (implicitly coercing as needed).

Static is where things (like types, classes, etc.) are determined at compile-time and cannot (generally) change at run-time, whereas a dynamic language does the checks at run-time.

Those definitions aren't exactly all that precise, though, and there's likely to be some overlap between them, so take it with a grain of salt.

In any case, when I was referring to strong/weak I was referring to explicit/implicit coercion. Python, JS, and Arc are all dynamically typed.

Amusingly, Wikipedia lists Arc as a strongly typed language. I definitely consider + (in Arc) to be weakly typed.