~technomancy/fennel

Make (.. 1) return "1" and not 1 v1 PROPOSED

Rudolf Adamkovič: 1
 Make (.. 1) return "1" and not 1

 2 files changed, 12 insertions(+), 1 deletions(-)
Andrey Listopadov <andreyorst@gmail.com> writes:
Next
Phil Hagelberg <phil@hagelb.org> writes:

The suggested patch makes the behavior consistent with Lua, in that
numbers are converted to strings, from 1 to any number of arguments, and
also consistent with Lisp, where 0 arguments result in returning the
identity value.

So, we have:

(..)         -> ""    -> correct in Lisp
(.. "1")     -> "1"   -> correct in Lisp
(.. "1" "2") -> "12"  -> correct in Lisp and Lua
(.. 1)       -> "1"   -> correct in Lisp with Lua coercion
(.. 1 2)     -> "12"  -> correct in Lisp with Lua coercion

All this is correct and expected in a Lisp dialect that obeys Lua type
coercion rules.

The patch breaks e.g. concatenating a table with itself, but such an
operation, let alone its return value, does not make any sense.

Rud
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~technomancy/fennel/patches/40974/mbox | git am -3
Learn more about email & git

[PATCH] Make (.. 1) return "1" and not 1 Export this patch

---
 src/fennel/specials.fnl |  2 +-
 test/core.fnl           | 11 +++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/fennel/specials.fnl b/src/fennel/specials.fnl
index ff2925f..adc515a 100644
--- a/src/fennel/specials.fnl
+++ b/src/fennel/specials.fnl
@@ -864,7 +864,7 @@ Method name doesn't have to be known at compile-time; if it is, use
               "Arithmetic operator; works the same as Lua but accepts more arguments."))

(define-arithmetic-special "+" :0)
(define-arithmetic-special ".." "''")
(define-arithmetic-special ".." "''" "''")
(define-arithmetic-special "^")
(define-arithmetic-special "-" nil "")
(define-arithmetic-special "*" :1)
diff --git a/test/core.fnl b/test/core.fnl
index 2236851..9b6d75e 100644
--- a/test/core.fnl
+++ b/test/core.fnl
@@ -54,6 +54,16 @@
  (== (let [f (fn [] (tset tbl :dbl (+ 1 (or (. tbl :dbl) 0))) 1)]
        (< 0 (f) 2) (. tbl :dbl)) 1))

(fn test-concatenation []
  (== (..) "")
  (== (.. "a") "a")
  (== (.. "a" "b") "ab")
  (== (.. "a" "b" "c") "abc")
  (== (.. 1) "1")
  (== (.. 1 2) "12")
  (== (.. 1 2 3) "123")
  (== (.. "a" "b" "c" 1 2 3) "abc123"))

(fn test-functions []
  (== ((fn [...] (: ... :gsub :foo :bar)) :foofoo) "barbar")
  (== ((fn [a b c] (+ a b c)) 1 (values 8 2)) 11)
@@ -398,6 +408,7 @@
{: test-booleans
 : test-calculations
 : test-comparisons
 : test-concatenation
 : test-conditionals
 : test-core
 : test-destructuring
-- 
2.40.1
Rudolf Adamkovič <salutis@me.com> writes: