Generating a vector of a specific length
atroche opened this issue · 2 comments
I want to generate vectors with exactly one item in them. Is this possible using herbert?
The default interpretation of something like [int] is [int+]. It used to be stricter about [int] meaning exactly one, but it was such a common misunderstanding (and difference from a lot of type systems) that I decided to support the automatic + interpretation. The downside is that "exactly one" is harder to say. You can use the & op (meaning "inline pattern") to get the "exactly one" match.
(h/conforms? '[(& int)] [1 2])
;=> false
(h/conforms? '[(& int)] [1])
;=> true
I should also comment that [int int] matches a sequence of exactly two ints. Only the single nested pattern has the automatic + interpretation, and only when the pattern is a symbolic pattern. Constants don't get the auto+. As I said that was a pragmatic decision based on common user errors.