I’m trying to write a function that optionally take a function as argument
let xxx ?(extractor = (fun a -> a)) yyy = ...
This ends up having type:
val xxx: ?extractor:('a -> 'a) -> 'c -> ...
My intention is to have the extractor to be a function that extract information from a structure, so the return type can be anything, but I want the default to be identity function
I tried to change the signature of it in the mli as
val xxx: ?extractor:('a -> 'b) -> 'c -> ...
But it does not compile, saying that (‘a -> ‘a) is not compatible with (a’ -> ‘b). I find it strange that (‘a -> ‘a) is not subset of (a’ -> ‘b). Is there a syntax that I can put in the mli file to say (‘a -> *) ?
In a functional language, an unspecified (variable) type you get back from a function basically has to appear somewhere in the type that you pass in. Otherwise, where would the value come from? So there are no (useful) functions of type
'a -> 'b.It might help to think about the types that you’re expecting to pass to your function, and how the extractor function would relate to them. It should be possible to have a fairly straightforward relation. Fighting with the type system usually means you’re trying to do something that is likely to fail unexpectedly, which is what the type system is trying to prevent.
Edit:
Maybe what I’m trying to say is that
'aand'b, the input and output types of your extractor function, aren’t going to be arbitrary types. The input type'ais probably related somehow to the type'c. Similarly the result type'bis probably going to be related to the return type of the functionxxxas a whole. Without a little more info about these relationships, it’s hard to make suggestions.If you try to think of them as independent, arbitrary types you run into the parametric impossibility that I was talking about above. (It also requires advanced typing, rank 2 polymorphism I think it is.)
As a simple example, say that
'aand'care the same type and that'bis the return type of the function. Then your function would pretty much have to look like this:This has the behavior you wanted: the default extractor function is the identity function. But this also forces the return type of
fto be the same as its input type. So the type of xxx is:Unless you want to get extremely fancy, there’s really no way around this, and I suspect a fancy solution would introduce complexities that would outweigh the convenience of having the optional parameter.
Maybe it would work to have two functions. To continue the simplified example they would look like this:
I think these have the types you want, and the only inconvenience is that you have two different names.