This is something I wanted to do from the first days of Skila, however the engine in Skila-1 was such a patchwork that it died before it was possible. Skila-3 finally has both kinds of sets:
let y *Submarine & *Plane = ••• let x *Submarine | *Plane = •••
The first line declares an intersection — we are making here some really super vehicle which can fly
, dive
and speedUp
, because it has all the members of given element types.
On the other hand, the second line builds somewhat limited vehicle using a type union — we can only speedUp
, because it has only common members, which are present in each of the given element type.
Is it practical in real life or is it just a fancy academia stuff? The former, the lack of set types in C# is killing me — I can mitigate the problem a little when I am getting data:
void loan<V>(V vehicle) where V : Submarine, Plane
which gives me an intersection, but I am toasted when it comes to the output:
V create<V>() where V : Submarine, Plane
It breaks the responsibility rule, because caller of create
has to specify concrete type and inside create
we have no chance to really create it (except for trivial case with default constructor).
And I have to cope with it all the time when writing wrappers for devices — I take them specifying their capabilities as constraints but I am unable to specify the capabilities of what I create (unless I am willing to create ton of interfaces — TelescopeWithThis
, TelescopeWithThat
).