Question about modules
I'm playing around with modules and having some troubles. This is just a toy example, but it will show you my problem. (Note, this is ReScript syntax, but you get the idea.)
module type Thing = {
type t
type fromT
let from: fromT => t
let into: t => option<fromT>
}
module StringThing: Thing = {
type t = string
type fromT = int
let from = i => Js.Int.toString(i)
let into = s => Belt.Int.fromString(s)
}
let s = StringThing.from(11) // Error!
The idea is just to have some little wrapper module that can take some type and convert. Now, this is sort of a pointless thing to do, but I'm just trying to figure out the compiler error. Here is the error:
This has type: int
Somewhere wanted: StringThing.fromT
What I'm confused about is I've set StringThing.fromT to int in the module definition, so I would expect the type system to realize what I'm trying to do...but it isn't! Can someone explain?
Posted Feb 13, 2021, 11:04 PM UTC
When you say
module StringThing: Thing = ..., you aren't just saying thatStringThingis compatible with the typeThing. You're constraining it to have exactly the typeThing. And inThing,fromTisn't= int, it's abstract.You might want to say
module StringThing: Thing with type fromT = intinstead. This means that instead of using the module typeThing, you use a new module type that's just likeThingbut which additionally exposes thatfromT = int.Thanks for the
with typesuggestion...that works fine. I'm having trouble finding it in Reason or ReScript docs though. Is there a way to do thewith typeannotation when you have a module and it's signature split between a.reand.reifile?Oh, yeah, I don't see it either. I know about it because I'm coming here from OCaml, but I'm not sure how you'd find out about it from Reason docs directly.
You mean, like if you had a
.reifile whose contents were just?
Then you could change the specification of
fromTto saytype fromT = int.