News for hackers. Stuff that matters.
Re:Modern JavaScript Tutorial by grondilu on Saturday September 16, 2017 @08:47AM (#15264409)
Question:

I started a JS project lately, I tried to write it as modernly as possible (using as many ES6 features[1] as I could), and yet I stumbled on difficulties that I blame to the lack of type system and polymorphism.

I'm considering writing my project in a typed language that compiles to JS, such as Scala or Haskell. But I would have to learn that language basically from the start.

Is it a good idea and if so which language would you recommend?

Re:Modern JavaScript Tutorial by anuragsoni on Saturday September 16, 2017 @08:56AM (#15264451)
Have you tried using typescript? That would be the closest you can get to a language that feels like JavaScript(it's a superset) and it has good tooling, types, and inter-op with JavaScript.

If you still want to try a language that compiles to JavaScript, I'd suggest taking a look at elm too. It has good documentation. It might lack some features from Haskell but in my opinion it is still a fun interesting language to learn.

Re:Modern JavaScript Tutorial by livelyar on Saturday September 16, 2017 @08:56AM (#15264452)
I mainly use JavaScript at work but have now began to move to TypeScript. I like it because I'm most familiar with JS and can reuse most of that knowledge, but I have the advantage of types (amount other things) and I still get JS in the end.
Re:Modern JavaScript Tutorial by diminish on Saturday September 16, 2017 @09:09AM (#15264503)
> difficulties that I blame to the lack of type system

What are they exactly?

Re:Modern JavaScript Tutorial by dpwm on Saturday September 16, 2017 @10:10AM (#15264711)
Have a look at OCaml. There's a project called BuckleScript that provides an OCaml to JavaScript compiler that maps OCaml quite cleanly to Javascript. Because BuckleScript is written in OCaml, it can also compile itself, resulting in the demo at [0]. Playing with that demo and looking at the output convinced me to give OCaml with BuckleScript a chance.

I was completely disillusioned with with JS. Thinking that types were the solution I tried TypeScript and found that I wanted it to be something it was never meant to be and just went back to JS. So far I've successfully used OCaml+BuckleScript with Mithril for a few personal projects and it's been really fun. I felt able to predict my productivity after only about 20 hours of use. If you do have any JS libraries that you can't write BuckleScript bindings for, or you can't figure out how to write bindings for them, you can always write a small JS shim that you can easily write bindings for.

OCaml isn't as dogmatic about purity as Haskell and unlike Haskell you're not encouraged to write type signatures for most functions. There's also far less of OCaml to learn than Haskell or Scala. All round it's a really good general purpose language.

There are two incompatible attempts at better standard libraries (core and batteries) and each encourage you to write OCaml in a style that does not have the feel of OCaml. The OCaml standard library itself is a collection of datatypes and depending on what you're doing may well be good enough.

There's also Reason [1], an alternative syntax for OCaml. Reason is undergoing heavy development at the moment and it appears that syntax changes are coming.

[0] https://bucklescript.github.io/bucklescript-playground

[1] https://reasonml.github.io/guide/what-and-why

Re:Modern JavaScript Tutorial by z3t4 on Saturday September 16, 2017 @12:47PM (#15265415)
You don't have to use all the syntax sugar, eg. array.concat vs ...spread And I guess its hard to stop thinking about types. its probably easier to go from assembly to JS, then from Java to JS ... In JS you just have to know string or number ... Although I'm no fan of it, you can annotate the variables like sName, nAge, sAddress, nCount, nId, sComment, nCommentId, etc. Or use - - (minus minus) when doing addition. Most other bugs will result in syntax errors or the classic "undefied is not a function" when trying to call push on an object that is not an array object.
Re:Modern JavaScript Tutorial by newsmania on Saturday September 16, 2017 @10:35AM (#15264798)
Would you elaborate on the issue? JavaScript actually does have a type system, it's just not very transparent. Using triple equals will not coerce types when comparing, whereas double equals will.
Re:Modern JavaScript Tutorial by blauditore on Saturday September 16, 2017 @10:45AM (#15264841)
Well, it does have a type system, but it's arguably the weakest kind of typing among all mainstream languages.
Re:Modern JavaScript Tutorial by make3 on Saturday September 16, 2017 @10:58AM (#15264904)
does Bash count as a mainstream language? :P (just kidding)
Re:Modern JavaScript Tutorial by paulddraper on Saturday September 16, 2017 @05:18PM (#15266818)
Yes. SQL too.

Neither are intended to be general purpose languages though.

Re:Modern JavaScript Tutorial by oneweekwonder on Saturday September 16, 2017 @10:24AM (#15264762)
Using the google closure compiler[0], js* has a type system. You must just be willing to write it in the docs.

[0]: https://github.com/google/closure-compiler/wiki/Types-in-the...

Re:Modern JavaScript Tutorial by gyrgtyn on Saturday September 16, 2017 @03:31PM (#15266254)
An option for doing it without a transpiler or another language: https://sanctuary.js.org/
Re:Modern JavaScript Tutorial by sorokod on Saturday September 16, 2017 @08:52AM (#15264432)
Kotlin
Re:Modern JavaScript Tutorial by Can_Not on Saturday September 16, 2017 @09:49AM (#15264628)
Any tutorials or starter boilerplates on using kotlinjs or bucklescript with vuejs would be much appreciated.
Re:Modern JavaScript Tutorial by grondilu on Saturday September 16, 2017 @08:56AM (#15264454)
Never heard of it. Definitely intriguing. Thanks.
Re:Modern JavaScript Tutorial by make3 on Saturday September 16, 2017 @11:00AM (#15264911)
What kind of project is it?