Hello! I am considering adding ReScript to an app I’m working and started a test project to see if I could get some things running with our lab’s current web tools (Vue, D3). It’s worth noting I’m pretty new to web dev; I’ve mostly done Data Science/Computational Bio using Python, R, Perl, and bits of Java and Scala but we’ve added some D3.js to our workflow and people really like the results so I’m leaning into it. So, sorry in advance if I am missing something obvious - I couldn’t find what I was looking for after a few hours and this seemed like the place to go.
I’m a little confused by this section of the docs:
"But you can also optionally write down the type, aka annotate your value:
let score: int = 10
If the type annotation for score
doesn’t correspond to our inferred type for it, we’ll show you an error during compilation time. We won’t silently assume your type annotation is correct, unlike many other languages."
I know the type annotations are optional but I can’t help but take from this passage that they’re doing something (that could result in a compiler error). My misunderstanding is from looking at the JS code generated by these ReScript snippets:
// rescript
let typedIntSum = (a:int, b:int): int => a + b;
let typedIntSumNoAnnotation = (a, b) => a + b;
Which gives me:
// js
function typedIntSum(a, b) {
return a + b | 0;
}
function typedIntSumNoAnnotation(a, b) {
return a + b | 0;
}
I’m not sure where the annotations have done anything (besides the usefulness of documentation and my own understanding)
The JS code produced still accepts 1 + “99” and returns 199. Where would this inference and aforementioned checks be taking place in the edit/run/debug cycle?
If I am looking for a compiler-time check on things like this like I might get is Sacla:
scala> val addInt = (x: Int, y:Int) => x + y
|
val addInt: (Int, Int) => Int = $Lambda$2148/0x00000001008d1040@7126e26
scala> addInt(1, "99")
^
error: type mismatch;
found : String("99")
required: Int
… what would I do? Am I thinking about this all wrong or using the wrong tools? It might be something silly because I’ve never “mixed” a dynamic and static language before.
My goal is to be able to writer cleaner code with some sanity checks from a type system in our Vue apps, am I barking up the wrong tree?