Type combinations

# Peter Hall (18 years ago)

Is there any way to express an "intersection" type, analogous to a union type? I can see there may be fewer use cases that there are for union types, but they could be useful in the following cases:

  1. An argument must implement two interfaces, but it's not practical to introduce a 3rd interface to unify them as a single type.
  2. An argument must be some subtype of a given type, but also implement one or more additional interfaces.

A trivial example (using dubious syntax):

function replicateObject(obj:(DisplayObject & IClonable)):void { var copy:DisplayObject = obj.clone(); parent.addChild(copy); }

I know that proposals are closed now, but this seems simpler than - and complimentary to - union types.

Peter

# Lars T Hansen (18 years ago)

On Nov 24, 2007 3:37 PM, Peter Hall <peter.hall at memorphic.com> wrote:

Is there any way to express an "intersection" type, analogous to a union type?

None at present. I toyed with similar things for a while, being able to subtract types from unions and so on.

I can see there may be fewer use cases that there are for union types, but they could be useful in the following cases:

  1. An argument must implement two interfaces, but it's not practical to introduce a 3rd interface to unify them as a single type.
  2. An argument must be some subtype of a given type, but also implement one or more additional interfaces.

A trivial example (using dubious syntax):

function replicateObject(obj:(DisplayObject & IClonable)):void { var copy:DisplayObject = obj.clone(); parent.addChild(copy); }

I know that proposals are closed now, but this seems simpler than - and complimentary to - union types.

I think TG1 should look into various kinds of type constraints for the next iteration of the spec -- this would be interesting for the parameterized types, also. For now, proposals really need to be closed if we're going to finish the spec within a reasonable time. I will write up this as a ticket in Trac and then immediately resolve it as "Deferred"; the intent is that we will produce a document with deferred ideas by and by. There are lots of them.

# Peter Hall (18 years ago)

I think TG1 should look into various kinds of type constraints for the next iteration of the spec -- this would be interesting for the parameterized types, also. For now, proposals really need to be closed if we're going to finish the spec within a reasonable time. I will write up this as a ticket in Trac and then immediately resolve it as "Deferred"; the intent is that we will produce a document with deferred ideas by and by. There are lots of them.

--lars

Thanks.

I think the important thing is that the syntax for union types is designed with more complex set operations in mind. Otherwise we could end up with some really ugly syntax if they get added in a later revision.

Peter