6

I've got a class in my application - for simplicity let's assume it's defined like this:

type baseType() =
    member this.A = 5.

Also, I've got a lot of functions that take objects of this type as an argument. Moreover, some of them take an array of this type:

let myFun (xArr : baseType[]) =
    // ... do something inspirig ;)

Now I realized, that it would be nice to have another class which derives from "baseType". e.g.:

type inhType() =
    inherit baseType()
    member this.B = 8.

However, I can't use the arrays of inherited type with the functions like "myFun"

let baseArr = [| baseType() |]
let inhArr = [| inhType() |]

myFun baseArr
myFun inhArr // won't work

which would be "nice to have". Is there a simple way to re-use my functions without applying so many changes ?

I guess one of the solution is to map my array using e.g. the function (fun (d : inhType) -> d :> baseType), but I'm wondering if there's anything else could be done.

2 Answers 2

10

You need to annotate your function as accepting a flexible type.

type A() = class end
type B() = inherit A()

let aArr = [| A() |]
let bArr = [| B() |]

// put # before type to indicate it's a flexible type
let f (x : #A[]) = ()

f aArr
f bArr  // works!
0
3

You can also populate an array of A with instances of a subtype, B, by using a type annotation:

let bArr: A[] = [| B() |]

This could be useful for one-off usage or if the function is in a third party library. Another common usage is creating boxed arrays (obj[]).

1
  • 2
    True, but note that this only works with array literals. Something like let bArr : A[] = Array.create 1 (B()) won't work.
    – kvb
    Aug 15, 2013 at 15:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.