News for hackers. Stuff that matters.
Re:Z – A dynamic functional programming language that compil... by stabbles on Sunday September 15, 2019 @05:13AM (#20976314)
Z performs coercion, but it's coercion rules make more sense than JavaScript's:

    "9" + 2 # ==> 11
    "9" ++ 2 # ==> "92"

Yeah, that just doesn't make things better
Re:Z – A dynamic functional programming language that compil... by uhryks on Sunday September 15, 2019 @06:04AM (#20976444)
Why not? It's not entirely explicit but it seems that the `++` operator is only for string concatenation, so using it with anything else that a string means it's going to be converted to string. I'm just curious about how the compiler enforces it.
Re:Z – A dynamic functional programming language that compil... by mantap on Sunday September 15, 2019 @07:37AM (#20976781)
What is the result of "badger"+2

If + is to be defined on a string and a number then the only sensible meaning is string concatenation. All numbers can be converted to strings but most strings cannot be converted to numbers.

There is a good reason to not define it because it is not associative: in JavaScript 2+2+"x" != 2+(2+"x")

Re:Z – A dynamic functional programming language that compil... by judofyr on Sunday September 15, 2019 @07:55AM (#20976828)
> What is the result of "badger"+2

The same result as `convert_to_number("badger") + convert_to_number(2)`. In Z it seems that converting an invalid string to number yields NaN, so "badger" + 2 returns NaN.

> There is a good reason to not define it because it is not associative: in JavaScript 2+2+"x" != 2+(2+"x")

The problem with + not being associative in JavaScript is because it's actually two different operators: String concatenation and number addition. Z doesn't have this problem because + is only number addition.

Personally I prefer strong typing (and thus `string + number` throwing an error and/or being a compile-time error), but it seems like the way Z does coercing is very sensible and doesn't have the same issues as JavaScript.

Re:Z – A dynamic functional programming language that compil... by zbentley on Sunday September 15, 2019 @01:35PM (#20978367)
I agree. I kind of hate to say this, but Perl (5) got this right by having an explicit concatentation operator for strings (given how common they are in many programming tasks), and then a separate addition operator.
Re:Z – A dynamic functional programming language that compil... by knubie on Sunday September 15, 2019 @02:14PM (#20978605)
This seems to be the case for Z, too, doesn't it?
Re:Z – A dynamic functional programming language that compil... by claudiawerner on Sunday September 15, 2019 @02:14PM (#20978603)
Correct me if I'm wrong, but PHP also has the same with the . notation, though perhaps because it was inspired by Perl.
Re:Z – A dynamic functional programming language that compil... by paulddraper on Sunday September 15, 2019 @05:49PM (#20979682)
It makes things better.

++ is for concatenation.

+ is for arithmetic.

Coercion or not, it's always puzzled me when languages used the same symbol.

I guess they're both associative and have identity values, but that's about it. One is commutative, the other is not.

* makes about as much sense for concatenation as +.