Live data from Hacker News

Grain: A strongly-typed functional programming language for the modern web

grain-lang.org

141–150 of 156 posts

Re: Grain: A strongly-typed functional programming language for the modern web

#141

If it's not too late to change, might I suggest doing away with the 'let rec ... and ...' syntax? The way to group mutually recursive bindings can be determined by finding the strongly connected components of the graph of the references between functions, so users don't need to manually specify it.

I do like the explicitness of marking recursive functions with 'rec', however. What would the syntax look like if it was no longer required?

Re: Grain: A strongly-typed functional programming language for the modern web

#142
post #9
post #3

Dart anybody?

Grain is quite different. Grain is more functional (no classes or context, tuples). Dart compiles to Javascript. Grain compiles to web assembly. Dart requires you to define types. Grain provides type safety and zero runtime errors without ever defining types manually.

So it's more like elm then?

Re: Grain: A strongly-typed functional programming language for the modern web

#143
post #120

If it's not too late to change, might I suggest doing away with the 'let rec ... and ...' syntax? The way to group mutually recursive bindings can be determined by finding the strongly connected components of the graph of the references between functions, so users don't need to manually specify it.

But then how do you shadow previous bindings? let f x = if (x == 0) then 0 else (f x)

Potentially controversial opinion: shadowing bindings is a bad idea.

Re: Grain: A strongly-typed functional programming language for the modern web

#144

Earlier quoted context omitted.

I think JavaScript simply got it quite wrong by trying to be way too clever. In theory it is fine to say that dividing by 0 is Infinity but what can you do with Infinity? In practice that causes the output of your program be either Infinity or NaN but the problem is that is usually never what you want. Then you want to know "Why do I get a NaN here?" and you will have to study all of your program and single step its…

> I think JavaScript simply got it quite wrong by trying to be way too clever. In theory it is fine to say that dividing by 0 is Infinity but ... This wasn't javascript's idea. They're just following the spec. https://en.wikipedia.org/wiki/IEEE_754#Exception_handling

This IEEE spec is about Exceptions like exception for division by zero. As far as I can tell dividing by zero in JavaScript does not cause an exception, does it?

Re: Grain: A strongly-typed functional programming language for the modern web

#145
post #124

Earlier quoted context omitted.

This seems like such an obvious solution it makes me wonder why creators of Haskell did not think of it? Lists are a very basic type used all the time if you don't get that right lot of the value of the great type-system is lost I would think.

They did. The elm-style version is in the standard libraries as: listToMaybe :: [a] -> Maybe a ( http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-M... )

That's a lengthy name for the simple operation of getting the first element of a list if there is one. I would have preferred 'car' :-)

Re: Grain: A strongly-typed functional programming language for the modern web

#146

Earlier quoted context omitted.

> This is something I wonder about JS: there are a lot of places that exceptions happen in (say) Python that just return `NaN` or `undefined` in javascript. Is it intentional? Is it a good idea? Yes and no respectively. It's intentional in the sense that the original Javascript was not intended to fault (much) because it was for basic scripting, so if one small script blew up it should not bring down the entire page'…

Invalid JSON parsing as `null` would be an exciting API decision, haha. One thing I think I like about JS over Python is that empty containers are "truthy". This, combined with non-raising missed lookups, tends to be pretty ergonomic. For me, at least. Though it is messier -- in Python `key in d` is well entrenched as an idiom, and in JS `if(d.key)` misfires if the value is zero, and `if(d.key === undefined)` doesn't…

> Invalid JSON parsing as `null` would be an exciting API decision, haha.

PHP's json_decode does that. And yes `null` is also returned as a PHP NULL.

> Though it is messier -- in Python `key in d` is well entrenched as an idiom, and in JS `if(d.key)` misfires if the value is zero

Or false, or null, or the empty string, or NaN.

FWIW `if (key in d)` also works in JS if d is a native object, though you need `d.has(key)` if d is an actual Map object instead.

Re: Grain: A strongly-typed functional programming language for the modern web

#147

Earlier quoted context omitted.

Well, the languages I'm using are Ruby, Python, Elixir, JavaScript so I definitely like not to write types. Still, I don't know how types work in Grain. The documentation doesn't say anything about it or it's very well hidden.

Are you asking how they are implemented? You'll have to read the compiler code for that. But the documentation does tell you... "No runtime type errors, ever. Every bit of Grain you write is thoroughly sifted for type errors, with no need for any type annotations." So you never write them, but you get all the benefits as if you did.

Reading the compiler code... well, I think I'll wait they write the documentation.

But how's that possible in general? Example, JSON parsing of data from a HTTP request. This is pseudo code

   # request.data is
   # {"a":1, "b":2, "c":"a string"}
   data = parse(request.data) 
   total = data["a"] + data["b"] # 3
   total = total + data["c"] # ops!
The last line is either a compiler error (how? forbidding input is not an option) or a runtime error (Grain doesn't have that) or what?

Re: Grain: A strongly-typed functional programming language for the modern web

#148

The main page doesn't do a great job of showing what's interesting about Grain vs. JavaScript. The only hint is this: "No runtime exceptions, ever. Every bit of Grain you write is thoroughly sifted for type errors, with no need for any type annotations." Maybe show some examples of errors Grain would catch that JavaScript wouldn't, like Elm does: http://elm-lang.org/

> No runtime exceptions, ever. This is something I wonder about JS: there are a lot of places that exceptions happen in (say) Python that just return `NaN` or `undefined` in javascript. Is it intentional? Is it a good idea? Examples: the multiplication operator essentially never throws. Out-of-bounds (or "not found") lookups don't throw. I suspect the logic is "only throw if you have the wrong type for that operation…

Generally, I'd say that the less strict the compiler/interpreter is, the worse code you'll end up with. In my experience, Python needs a lot of external checks (i.e. linting, static analysis) to help the programmer write code that actually work as intended (and JS probably is worse).

As John Carmack said: "...if you have a large enough codebase, any class of error that is syntactically legal probably exists there."

Re: Grain: A strongly-typed functional programming language for the modern web

#149

Earlier quoted context omitted.

> I think JavaScript simply got it quite wrong by trying to be way too clever. In theory it is fine to say that dividing by 0 is Infinity but ... This wasn't javascript's idea. They're just following the spec. https://en.wikipedia.org/wiki/IEEE_754#Exception_handling

This IEEE spec is about Exceptions like exception for division by zero. As far as I can tell dividing by zero in JavaScript does not cause an exception, does it?

Yes, it does. And the result of that exception produces a signed infinity, unless the numerator was zero. Note that this is different than the language-level Error construct that can be thrown and caught.

Re: Grain: A strongly-typed functional programming language for the modern web

#150

Has anyone found in the sources if/how they treat garbage collection?

Yes. They currently use reference counting since WASM doesn't allow stack inspection.

Do you have a link to the source?
Post reply on HN