Fix string multiplication with a value between 0.0 and 1.0#2142
Fix string multiplication with a value between 0.0 and 1.0#2142nicowilliams merged 2 commits intojqlang:masterfrom
Conversation
9334a4a to
646e859
Compare
|
This is makes it behave as same as 1.6, but I personally prefer |
|
@itchyny - There are certainly good arguments for evaluating String * 0 as "", so much so that I would be happy to open a “bug report” to that effect if you would be willing to make the change. As things are, I often define a helper function to work around the problem. Of course, it’s easy enough to use // but it’s so easy to forget that jq somehow got this one wrong…. |
|
I'd like to remove string multiplication, and any arithmetic operations on strings other than addition. Too much polymorphism in a dynamically typed language is a mistake ***@***.*** convinced me of this years ago).
|
|
@nicowilliams wrote:
Well, that particular horse is out of the barn, grazing in some Anyway, STRING * NUMBER is well-understood and quite widely adopted.(*) The thing that is really questionable is Since that horse too is out of the barn, I would propose that (at least pending jq 2.0): a) q.v. #2672 (*) Courtesy of ChatGPT: Python: Ruby: JavaScript: Perl: PHP: Elixir: |
This is one reason I'd like to have builtin modules, and then we could have |
src/builtin.c
Outdated
| jv res = jv_null(); | ||
| int n = jv_number_value(num); | ||
| double d = jv_number_value(num); | ||
| int n = 0.0 < d && d < 1.0 ? 1 : d; |
There was a problem hiding this comment.
Questions while we're here:
- if
("string" * 0) == null, why should("string" * 0.1) == "string"? - shouldn't
("string") * 0 == ""?nullseems very wrong - currently multiplying by a negative number,
nan,-nan,infinite, or-infiniteyieldsnull-- why not an error, orempty? (well,emptywould be harder to arrange here, so let's not go there)
This commit fixes a regression of 6306ac8.
Yes!!! I think we're all (?) agreed about that. Let's do it!
Based on the results (i.e. jq), I think @stedolan sought to avoid raising errors, preferably by giving some meaning to otherwise meaningless expressions (this would help explain the liberal doses of polymorphism), or preferring In the present case, it probably doesn't make much practical difference, except for backwards compatibility. Ergo, I'd stick with But let's not make the perfect be the enemy of the good. |
Well, that was before we had a way to catch errors, but sure. |
646e859 to
abbc482
Compare
abbc482 to
075161d
Compare
|
Thanks, I fixed to emit an empty string when repeating a string by 0 (and less than 1). The logic here can be optimized by copying the written string to grow twice and twice, but could lead to another bug so let's do later. |
|
Thanks! |
|
@nicowilliams wrote:
One feature (or per @itchyny, either a misfeature or a non-feature) of jq is that jq allows This seems to be somewhat relevant here, as I'll illustrate in the case of Let's suppose that we want to make it easy for someone using jq 1.7 to invoke (or have their existing code use) the 1.6 version. Then we could retain the 1.6 version by adding the
2 (a). add This last allows users to have their cake (existing code does not need to be modified) and eat it (use the new walk at the same time, too). One important question, though, remains: Is this ability to use :: prefixes in this way a bug, a feature, or something else? |
This commit fixes a regression of 6306ac8 (#2118).
Based on the discussion, we change the behavior ofjq -n '0.5 * "abc"'should print"abc"notnull.string * numberto emit""when0 ≦ number < 1.