Hello,
I’m exploring how rescript can be used along with typescript (in order to ease adoption at work) and I can’t figure out how to solve this problem :
- define a
logger
that has several “methods” (likeinfo
,error
etc) they use a generic type so it’s something likeinfo: ('a, string) => 'a
(like the Debug.Trace in Haskell) - I have some business logic that takes this logger type as parameter so I don’t have to explicitly pass every method (like an interface in OO, a record of functions in Haskell)
The overall idea is to have an hexagonal architecture, the whole domain modelling part (“usecase + domain”) would be in rescript while the adapters & main would be in typescript.
I have 2 failed attempts but for 2 different reasons
version 1 : using a functor, I can do something like :
module type Logger1 = {
let info: ('a, string) => 'a
let error: ('a, string) => 'a
}
module BusinessLogic1 = (Logger: Logger1) => {
@genType
let do = (age: int): unit => {
age->Logger.info("hello")->ignore
"aString"->Logger.info("hello!")->ignore
()
}
}
problem : it looks nice on the rescript side… but genType generates nothing in ts
version 2 : I try to pass everyting as parameter
module Logger2 = {
type info<'a> = ('a, string) => 'a
type error<'a> = ('a, string) => 'a
// type info = ('a, string) => 'a <- this doesn't work because 'a is unbound...
}
module BusinessLogic2 = {
@genType
let do = (age: int, logInfo: Logger2.info<'a>): unit => {
age->logInfo("hello")->ignore
//"aString"->logInfo("hello")->ignore // this doesn't work because 'a has been inferred as int
()
}
}
problem : it doesn’t really solve the problem on the rescript side… but it generates ts code