4 ms·
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 difficul
by grondilu 9y ago
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?
- newsmania 9y agoWould 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.
- blauditore 9y agoWell, it does have a type system, but it's arguably the weakest kind of typing among all mainstream languages.
- make3 9y agodoes Bash count as a mainstream language? :P (just kidding)
- paulddraper 9y agoYes. SQL too. Neither are intended to be general purpose languages though.
- sorokod 9y agoKotlin
- anuragsoni 9y agoHave 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.
- livelyar 9y agoI 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.
- diminish 9y ago> difficulties that I blame to the lack of type system What are they exactly?
- dpwm 9y agoHave 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 https://bucklescript.github.io/bucklescript-playground [1] https://reasonml.github.io/guide/what-and-why https://reasonml.github.io/guide/what-and-why
- oneweekwonder 9y agoUsing 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-Closure-Type-System https://github.com/google/closure-compiler/wiki/Types-in-the...
- make3 9y agoWhat kind of project is it?
- z3t4 9y agoYou 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.
- gyrgtyn 9y agoAn option for doing it without a transpiler or another language: https://sanctuary.js.org/ https://sanctuary.js.org/