Skip to content

Restructure into spec to better fit possible usages #64

Open
@stanislowskij

Description

@stanislowskij

into is simultaneously under-spec'd and over-spec'd right now due to some weird edge cases that come with the function. Officially, the documentation notes that, with two arguments, into should take two collections, but there are several cases where this is not true.

  • If to is any value or expression, and from is an empty collection, (into to from) returns evaluation of to.
user=> (into :a [])
:a

⚠️ Incorrectly fails Babel spec:

babel.middleware=> (into :a [])
The first argument of (into :a []) was expected to be a sequence but is a keyword :a instead.
  • If from is a non-empty collection, (into nil from) performs conj on (list (first from)) and the rest of the list, regardless of what the type of collection from is.
user=> (into nil [:a :b])
(:b :a)

Same result in Babel.

  • If to is a string, and from is a non-empty string, (into to from) throws an exception.
user=> (into "" "test")
Execution error (ClassCastException) at user/eval1579 (REPL:1).
class java.lang.String cannot be cast to class clojure.lang.IPersistentCollection (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.IPersistentCollection is in unnamed module of loader 'bootstrap')

Correctly fails Babel spec, though might need to be improved, since the message is wrong (see below):

babel.middleware=> (into "" "test")
The first argument of (into "" "test") was expected to be a sequence but is a string "" instead.
  • Note that this still works if from is an empty string:
user=> (into "a" "")
"a"

⚠️ Incorrectly fails Babel spec:

babel.middleware=> (into "a" "")
The first argument of (into "a" "") was expected to be a sequence but is a string "a" instead.

  • Note also that Babel specs incorrectly pass if to is a collection and from is not. In these cases, we get an error on seq instead (which is also a problem. See: Need spec on seq #63).
babel.middleware=> (into [1 2 3] 4)
Don't know how to create a sequence from a number.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions