News for hackers. Stuff that matters.
Re:Type-safe GraphQL with OCaml by kbenson on Thursday November 30, 2017 @08:55PM (#15821339)
> The first is modular implicits: it feels so kludgy to have to type `a + b` for integers, `a +. b` for floats and `a ^ b` for string concatenation. I know it sounds like a small thing, but it makes the language feel inelegant, and aesthetics are important.

That's interesting, as being forced to use the same operator for addition and for concatenation (and what's more with the result of mixed types if allowed often dependent on the order of the parameters) has always seemed extremely inelegant to me.

Addition and concatenation are not the same thing. Why they should share a symbol when they don't share some properties integral to their nature (it's non-commutative) is beyond me, and I think it's caused a lot of bugs that didn't need to happen.

Not allowing the addition of floats and ints together is less of a fundamental issue, but it also helps quite a few problems. Al alternative solution, if you didn't want to reply on implicit coercion would be to explicitly convert one operand to the appropriate type so they match.

Or you can go whole hog and provide an entirely separate set of operators for different types, like Perl did (string concatenation is '.', and equivalents comparitors are eq,ne,gt,lt,ge,le). That works well in Perl's case, but that's mostly because for Scalars Perl really just wants to know whether you are treating it as a number or a string, so there's only two types to account for.

Re:Type-safe GraphQL with OCaml by baddox on Thursday November 30, 2017 @11:39PM (#15821953)
Couldn’t the type system still allow using the + operator, but requiring the values on both sides to have the same type? You could do more magic like having int + float return a float, but I’d even prefer typing “int.toFloat + float” than “int +. float”.
Re:Type-safe GraphQL with OCaml by masklinn on Friday December 01, 2017 @07:02AM (#15823333)
> Couldn’t the type system still allow using the + operator, but requiring the values on both sides to have the same type?

'course it could, multiple languages already do that e.g. Rust (https://play.rust-lang.org/?gist=530fa94d3f451e896ac0d3ddeaf...), Haskell (https://repl.it/repls/FrozenBlondUmbrellabird), Swift (https://repl.it/repls/LuckyNaiveBream)

Re:Type-safe GraphQL with OCaml by kbenson on Friday December 01, 2017 @12:29AM (#15822133)
Yeah, that's what I was referring to by explicitly converting one operand so they are like types. I too would prefer this in a language that takes typing of this sort seriously.
Re:Type-safe GraphQL with OCaml by runeks on Friday December 01, 2017 @11:07AM (#15824676)
> Not allowing the addition of floats and ints together is less of a fundamental issue, but it also helps quite a few problems.

Needing to use +. to add two floats seems odd to me as well, and I believe this is what OP was pointing out. I would agree with you that adding and int to a float should require conversion of one of the arguments, but I see no need for a separate + operator for floats and ints.

Re:Type-safe GraphQL with OCaml by kbenson on Friday December 01, 2017 @04:38PM (#15827783)
> I would agree with you that adding and int to a float should require conversion of one of the arguments, but I see no need for a separate + operator for floats and ints.

Yes, what I was trying to express is that in a typed language with many types, choosing a few types and using operators to signify what you are doing is an odd choice, especially if you implement it for some types but not others. I understand the want of the language designers to protect people from these mistakes, I just think they chose an oddly inconsistent way of of doing so. The logical conclusion of different operators for different types is many more operators than shown here, so that leaves the language with these special cases. Special cases cause confusion.

Re:Type-safe GraphQL with OCaml by fny on Friday December 01, 2017 @12:02AM (#15822054)
Isn't it also the case that floating point addition may not necessarily be commutative? I'd imagine there would need to be some standard to enforce it as such.
Re:Type-safe GraphQL with OCaml by jabot on Friday December 01, 2017 @01:23AM (#15822315)
No, addition is still commutative (i.e. a + b == b + a)

Sadly, it is not associative (i.e. (a+b)+c != a+(b+c))

Imagine a being a large number, and b and c being so small that adding each single one to a will result in a, but adding their sum will result in just the smallest increment over a.

Re:Type-safe GraphQL with OCaml by kbenson on Friday December 01, 2017 @01:36AM (#15822359)
Actually, floating point addition isn't necessarily commutative[1][2], which is one of the problems of thinking of floating point as just a number. Although in this case, it's at least still addition, but the type of things being added have special conditions.

It's more like doing calculations on scientific notation, where there are special rules for how to keep the right number of significant figures/precision after operations. Like scientific notation, you would probably convert to one form or another (even if in your head) before doing the operation, and keep note of the possible loss of precision.

This is exactly the reason why adding floats to ints is something that people think should require an explicit conversion, so it's more obvious that's a point that may introduce loss of accuracy.

1: https://stackoverflow.com/questions/24442725/is-floating-poi...

2: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BinMath/a...

Re:Type-safe GraphQL with OCaml by jabot on Friday December 01, 2017 @02:09AM (#15822455)
Urgh. C semantics. Well, at least that will not happen if you actually use IEEE754 _and_ if a, b, and c all have the same floating point precision _and_ there is no weird promotion/precision change going on.

Now that you mention, there was this fascinating denial-of-service vulnerability in PHP which is (kind of) related... https://news.ycombinator.com/item?id=2066352 http://www.exploringbinary.com/why-volatile-fixes-the-2-2250...

EDIT: To expand on this: The following code is valid, and may print "weird":

  double x = f();
  double y = x;
  if(x != 0 && y == 0) {
     printf("weird\n");
  }
... if x is stored in a 80bit floating point register, and y is stored in memory and therefore only has 64bit