Sub Type Method Invoke


So I have the following types:

extendable entity type type S {
ts: [T]

Type T {
doSomething: abstract member function(): S js server

entity type ChildT mixes T {
doSomething: ~ js server

if I say
var inst = S.fetch().objs[0];
inst.ts.forEach(func(i) {

I get an exception because doSomething has no definition on type T. Rather than using ChildT’s implementation. If I change S to have ts: ChildT, then it all works accordingly.

The goal is to have multiple ChildT variants that can all be stored inside S and called. What am I doing wrong?



The reason this doesn’t work is because when you do S.fetch, all returned instances will be of type S, even if their true subtype is T. When you then call a member function on that instance, it will only be possible if the member function is defined on type T.

If you want to call the member function on the actual type of each instance (rather than the fetched type), then you should call getSpecific on the instance of T. Then, assuming that its actual type is S, that will give you an instance of S that you can then call the function on.