[Proposal] Enum subsets

This is loosely related to but not meant to 'compete' with the ad hoc enum
proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose
members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](
http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum `LCDColorModel` with only
three colors, `red, blue, green` .

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

enum LCDColors {
case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior
from their similarly named cases in `Colors`. We would like, simply stated,
to explicitly restrict the cases allowed within a specific portion of our
software. There are, currently, a few approaches
1. Duplicate functionality in `LCDColors`
- Completely manually
- Protocols with 'minimal' manual duplication
2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors`
and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases
must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to existing
code.

7 Likes

This could be useful in categorizing and grouping within large enums ErrorType enums come to mind. Would there be any problem with making the subset more explicit? e.g.

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

extension Colors {
	subset  LCDColors  red, green, blue
}
¡¡¡

On Jun 3, 2016, at 7:22 AM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum `LCDColorModel` with only three colors, `red, blue, green` .

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

enum LCDColors {
	case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior from their similarly named cases in `Colors`. We would like, simply stated, to explicitly restrict the cases allowed within a specific portion of our software. There are, currently, a few approaches
  1. Duplicate functionality in `LCDColors`
    - Completely manually
    - Protocols with 'minimal' manual duplication
  2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors` and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name mangling be used which, along with the declaration order restriction should mean that all possible subsets have a stable and predictable name which contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I really like the idea behind this proposal.

Some questions:

- Would the enum 'slice' be a distinct type relative to the base enum?
- On a related note, would shared cases between the sliced enum and the
base enum be implicitly convertible?
- If they aren't implicitly convertible between each other, would there be
an affordance to perform conversions (e.g. a "parentEnumType" property and
an "init?(parentEnumType:)" initializer)/
- Would you be able to further slice a sliced enum? If so, would they share
the same parent, or would the 'parent' of the sliced sliced enum be the
sliced enum?
- If the parent enum has members that switch on 'self', would those members
be available to the child automatically?
- What happens if you have two (or more) slices with disjoint members?
(e.g. 'LCDColors' and 'TrafficLightColors') Would they be considered
completely separate 'sub-types'?

Best,
Austin

¡¡¡

On Fri, Jun 3, 2016 at 6:22 AM, T.J. Usiyan via swift-evolution < swift-evolution@swift.org> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc enum
proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose
members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](
http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum `LCDColorModel` with only
three colors, `red, blue, green` .

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

enum LCDColors {
case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior
from their similarly named cases in `Colors`. We would like, simply stated,
to explicitly restrict the cases allowed within a specific portion of our
software. There are, currently, a few approaches
1. Duplicate functionality in `LCDColors`
- Completely manually
- Protocols with 'minimal' manual duplication
2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors`
and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases
must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to
existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

2 Likes

I really like this idea, a few comments I have:
Would this subset be exhaustive within only the subset you’ve restricted it to? I’m hoping so! i.e- if I do a switch on LCDColors I assume I only have to provide three cases, but this doesn’t appear to be expressly mentioned.
Do you envision this being usable in function signatures without declaring a type alias first? I can see cases where it may be convenient to do so, but some of the concerns with ad-hoc enums has been over there being no formal declaration of the type.
Lastly, is the ordering of the labels really important? Can’t the compiler just reorder them behind the scenes if necessary? I don’t think there should be any problem with the compiler detecting that Colors.(violet|orange|green) is the same as Colors.(orange|green|violet). I mean it’d be good practice to maintain the same order of course, but I think it’s better to allow any order, as the parent type’s ordering may change and break stuff; I’m not sure if relying on the parent’s ordering is really wise anyway, if you need it for some purpose then it should be defined in a method.
You need to cover conversion between the two types. I assume that if I have an LCDColors case it will work anywhere that takes a case from Colors, but how does the reverse work? Can I assign an instance of Colors to LCDColors via casting for example (with a failure if it’s not one of the specified cases)?
But yeah, I like this idea, it’s kind of like an enum “slice”, and while it’s not strictly the same as ad-hoc enums it could solve many of their use-cases in a safer way.

¡¡¡

On 3 Jun 2016, at 14:22, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum `LCDColorModel` with only three colors, `red, blue, green` .

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

enum LCDColors {
	case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior from their similarly named cases in `Colors`. We would like, simply stated, to explicitly restrict the cases allowed within a specific portion of our software. There are, currently, a few approaches
  1. Duplicate functionality in `LCDColors`
    - Completely manually
    - Protocols with 'minimal' manual duplication
  2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors` and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name mangling be used which, along with the declaration order restriction should mean that all possible subsets have a stable and predictable name which contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I don't have a problem with something as explicit as that. I was mostly
avoiding adding keywords with my proposed syntax. I have basically no tie
to the proposed syntax.

TJ

¡¡¡

On Fri, Jun 3, 2016 at 12:32 PM, Christopher Kornher <ckornher@me.com> wrote:

This could be useful in categorizing and grouping within large enums
ErrorType enums come to mind. Would there be any problem with making the
subset more explicit? e.g.

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

extension Colors {
subset  LCDColors  red, green, blue
}

On Jun 3, 2016, at 7:22 AM, T.J. Usiyan via swift-evolution < > swift-evolution@swift.org> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc enum
proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose
members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](
http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum `LCDColorModel` with only
three colors, `red, blue, green` .

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

enum LCDColors {
case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior
from their similarly named cases in `Colors`. We would like, simply stated,
to explicitly restrict the cases allowed within a specific portion of our
software. There are, currently, a few approaches
1. Duplicate functionality in `LCDColors`
- Completely manually
- Protocols with 'minimal' manual duplication
2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors`
and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases
must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to
existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Since this seems to have some interest, I've made a gist.

Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose
members are strict subsets of explicitly defined enums.

Swift-evolution thread: Discussion thread topic for that proposal
<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160530/020025.html&gt;
Motivation

Consider a situation where we have an enum Color which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum LCDColorModel with only three
colors, red, blue, green .

enum Color {
    case red, orange, yellow, green, blue, indigo, violet
    â€Ś
}
enum LCDColor {
    case red, green, blue
}

The cases in LCDColor in our scenario do not require different behavior
from their similarly named cases in Color. We would like, simply stated, to
explicitly restrict the cases allowed within a specific portion of our
software. There are, currently, a few approaches
1. Duplicate functionality in LCDColor
- Completely manually
- Protocols with ‘minimal’ manual duplication
2. Avoid duplication by allowing conversion to Color.

Neither of these solutions make the subset relationship between Color and
LCDColor clear or strict.
Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColor = Color.(red|green|blue)

LCDColor has all of the type and instance methods of Color.

   - Barring any technical reason, the ‘actual’ name of the type, in this
   example, is Color.(red|green|blue) This makes the relationship to
Colorsyntactically
   clear. If a typealias is not desired, Color.(red|green|blue) should
   refer to the same type as LCDColor
   - Switching over Color.(red|green|blue) should only need to be
   exhaustive for the three cases .red, .green, and .blue.
   - Two initializers should be implicitly created
      - Color to LCDColor?
         - returns nil for all cases not in LCDColor
      - LCDColor to Color
         - Obvious and trivial implementation mapping cases from LCDColor
          to Color
      - Casting should be allowed
      - from superset to subset only using as? or as! syntax.
      - from subset to superset using as
   - Creating subsets of subsets is not allowed but reasonable conversions
   among subsets should be allowed if technically feasible such that:
      - Given subsets of C A and B, where A is a superset of B, the casting
      relationship between A and B should be similar to that between C and
      either of the other two named subsets.

Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.
If a mangled name approach is taken, the ordering of cases should be sorted
to ensure stability.
Alternatives considered

   - Do nothing. This feature is not strictly necessary but does allow for
   expressivity not currently available in the language.
   - implicitly create properties which convert to superset type.

Impact on existing code

This is an additive change which should have no breaking change to existing
code.

¡¡¡

On Fri, Jun 3, 2016 at 4:57 PM, Austin Zheng <austinzheng@gmail.com> wrote:

I really like the idea behind this proposal.

Some questions:

- Would the enum 'slice' be a distinct type relative to the base enum?
- On a related note, would shared cases between the sliced enum and the
base enum be implicitly convertible?
- If they aren't implicitly convertible between each other, would there be
an affordance to perform conversions (e.g. a "parentEnumType" property and
an "init?(parentEnumType:)" initializer)/
- Would you be able to further slice a sliced enum? If so, would they
share the same parent, or would the 'parent' of the sliced sliced enum be
the sliced enum?
- If the parent enum has members that switch on 'self', would those
members be available to the child automatically?
- What happens if you have two (or more) slices with disjoint members?
(e.g. 'LCDColors' and 'TrafficLightColors') Would they be considered
completely separate 'sub-types'?

Best,
Austin

On Fri, Jun 3, 2016 at 6:22 AM, T.J. Usiyan via swift-evolution < > swift-evolution@swift.org> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc
enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose
members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](
http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum `LCDColorModel` with only
three colors, `red, blue, green` .

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

enum LCDColors {
case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different
behavior from their similarly named cases in `Colors`. We would like,
simply stated, to explicitly restrict the cases allowed within a specific
portion of our software. There are, currently, a few approaches
1. Duplicate functionality in `LCDColors`
- Completely manually
- Protocols with 'minimal' manual duplication
2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors`
and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases
must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to
existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Your original syntax makes LCDColors clearly a type. The typealias obscures the fact that a new type is being created. An alternative might be something like:

enum LCDColors : Colors {
	case red, green, blue
}

or perhaps

enum LCDColors subset: Colors {
	case red, green, blue
}

This would would be compatible with something that I have been hoping for, creating superset enums from multiple enums for things like errors:

enum AppErrors  {
	// Defined by its subsets
}

enum NetworkingErrors  subset: AppErrors {
	case NWError1 = 1000, NWError2. NWError3
}

enum UserInputErrors  subset: AppErrors {
	case UIError1 = 2000, UIError2. UIError3
}

The compiler would have to check for rawValue collisions.

having the superset enum define values that could be used in children would allow:

enum AppErrors  {
	// Defined by its subsets
	case NetworkingErrorBase = 1000
	case UIErrorBase = 2000
}

enum NetworkingErrors  subset: AppErrors {
	case NWError1 = NetworkingErrorBase, NWError2. NWError3
}

enum UserInputErrors  subset: AppErrors {
	case UIError1 = UIErrorBase, UIError2. UIError3
}
¡¡¡

On Jun 3, 2016, at 10:39 AM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

I don't have a problem with something as explicit as that. I was mostly avoiding adding keywords with my proposed syntax. I have basically no tie to the proposed syntax.

TJ

On Fri, Jun 3, 2016 at 12:32 PM, Christopher Kornher <ckornher@me.com <mailto:ckornher@me.com>> wrote:
This could be useful in categorizing and grouping within large enums ErrorType enums come to mind. Would there be any problem with making the subset more explicit? e.g.

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

extension Colors {
	subset  LCDColors  red, green, blue
}

On Jun 3, 2016, at 7:22 AM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum `LCDColorModel` with only three colors, `red, blue, green` .

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

enum LCDColors {
	case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior from their similarly named cases in `Colors`. We would like, simply stated, to explicitly restrict the cases allowed within a specific portion of our software. There are, currently, a few approaches
  1. Duplicate functionality in `LCDColors`
    - Completely manually
    - Protocols with 'minimal' manual duplication
  2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors` and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name mangling be used which, along with the declaration order restriction should mean that all possible subsets have a stable and predictable name which contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

A few ideas:

* Mention the pull request about member sets?
* I'd try to give some more compelling use cases (Where a union type can be broadly applied across more specific uses, which then tend to narrow the applicable cases, and you want to limit (and compile check) those cases while inheriting behavior).
* Mention how enum specialization tends to limit possible cases, and consider how enum expansion/inheritance might be another direction for expanding existing enums (for example, when adding new phone models, but using an existing enum, with an extension), and how this might impact footprint in associated types.
* Discuss how behavior extensions on narrowed enums could be limited to the subset, so would be syntactically limited to the semantic subset as a motivation for why LCD colors might appropriately be used in a calibrate functions but the full color cases would not, etc.

-- E

¡¡¡

On Jun 3, 2016, at 3:35 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub
Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: Discussion thread topic for that proposal <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160530/020025.html&gt;
Motivation

Consider a situation where we have an enum Color which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum LCDColorModel with only three colors, red, blue, green .

enum Color {
    case red, orange, yellow, green, blue, indigo, violet
    â€Ś
}

enum LCDColor {
    case red, green, blue
}
The cases in LCDColor in our scenario do not require different behavior from their similarly named cases in Color. We would like, simply stated, to explicitly restrict the cases allowed within a specific portion of our software. There are, currently, a few approaches
1. Duplicate functionality in LCDColor
- Completely manually
- Protocols with ‘minimal’ manual duplication
2. Avoid duplication by allowing conversion to Color.

Neither of these solutions make the subset relationship between Color and LCDColor clear or strict.

Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColor = Color.(red|green|blue)
LCDColor has all of the type and instance methods of Color.

Barring any technical reason, the ‘actual’ name of the type, in this example, is Color.(red|green|blue) This makes the relationship to Colorsyntactically clear. If a typealias is not desired, Color.(red|green|blue) should refer to the same type as LCDColor
Switching over Color.(red|green|blue) should only need to be exhaustive for the three cases .red, .green, and .blue.
Two initializers should be implicitly created
Color to LCDColor?
returns nil for all cases not in LCDColor
LCDColor to Color
Obvious and trivial implementation mapping cases from LCDColor to Color
Casting should be allowed
from superset to subset only using as? or as! syntax.
from subset to superset using as
Creating subsets of subsets is not allowed but reasonable conversions among subsets should be allowed if technically feasible such that:
Given subsets of C A and B, where A is a superset of B, the casting relationship between A and B should be similar to that between C and either of the other two named subsets.
Detailed design

While I am unsure of the entirety of the design, I propose that name mangling be used which, along with the declaration order restriction should mean that all possible subsets have a stable and predictable name which contains all of the information necessary to infer cases.
If a mangled name approach is taken, the ordering of cases should be sorted to ensure stability.

Alternatives considered

Do nothing. This feature is not strictly necessary but does allow for expressivity not currently available in the language.
implicitly create properties which convert to superset type.
Impact on existing code

This is an additive change which should have no breaking change to existing code.

On Fri, Jun 3, 2016 at 4:57 PM, Austin Zheng <austinzheng@gmail.com <mailto:austinzheng@gmail.com>> wrote:
I really like the idea behind this proposal.

Some questions:

- Would the enum 'slice' be a distinct type relative to the base enum?
- On a related note, would shared cases between the sliced enum and the base enum be implicitly convertible?
- If they aren't implicitly convertible between each other, would there be an affordance to perform conversions (e.g. a "parentEnumType" property and an "init?(parentEnumType:)" initializer)/
- Would you be able to further slice a sliced enum? If so, would they share the same parent, or would the 'parent' of the sliced sliced enum be the sliced enum?
- If the parent enum has members that switch on 'self', would those members be available to the child automatically?
- What happens if you have two (or more) slices with disjoint members? (e.g. 'LCDColors' and 'TrafficLightColors') Would they be considered completely separate 'sub-types'?

Best,
Austin

On Fri, Jun 3, 2016 at 6:22 AM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
This is loosely related to but not meant to 'compete' with the ad hoc enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum `LCDColorModel` with only three colors, `red, blue, green` .

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

enum LCDColors {
	case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior from their similarly named cases in `Colors`. We would like, simply stated, to explicitly restrict the cases allowed within a specific portion of our software. There are, currently, a few approaches
  1. Duplicate functionality in `LCDColors`
    - Completely manually
    - Protocols with 'minimal' manual duplication
  2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors` and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name mangling be used which, along with the declaration order restriction should mean that all possible subsets have a stable and predictable name which contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I like the idea, conceptually—having more set-like semantics for enums
sounds like it would open up some interesting possibilities. (And I don't
think we should limit ourselves to subsets either. Supersets are also
interesting.)

Things could get pretty tricky though, so there are a lot of gaps to fill
in here. Off the top of my head, I'd be curious about the following
situations:

* You don't mention cases with associated values anywhere. Is it your
intention to prohibit those, or would the following work?

enum Foo {
   case Bar
   case Baz(Int)
   case Blorp(String)
}
typealias FooSubset = Foo.(Bar|Baz)
// FooSubset is effectively: enum FooSubset { case Bar; case Baz(Int) }?

I can't think of any reason off the top of my head to prohibit cases with
associated values, for what it's worth.

* How would non-case members of an enum (i.e., computed properties,
methods, etc.) be effected? Other folks in the thread have mentioned
methods that switch on self. In a very abstract sense, there are two kinds
of these properties/methods: those that simply use "self" to branch, and
those that use the enum in other ways. For example, I see no reason that
this shouldn't work:

enum Color {
  case red
  case blue
  case green

  var uiColor: UIColor {
    switch self {
      case .red: return UIColor.red()
      case .blue: return UIColor.blue()
      case .green: return UIColor.green()
    }
  }
}
let x: Color.(blue|green) = Color.blue
let color = x.uiColor

But on the other hand, you could have something like this:

enum Color {
  case red
  case blue
  case green

  var nextColor: Color {
    switch self {
      case .red: return .blue
      case .blue: return .green
      case .green: return .red
    }
  }
}
let x: Color.(blue|green) = Color.blue
let color = x.nextColor // umm....

Things start getting really hard to predict because it's very hard to
reason about what happens inside these computed properties, and I fear that
the solution becomes "conservatively exclude a lot of stuff" and that
starts taking a lot of the power away from those enums.

¡¡¡

On Fri, Jun 3, 2016 at 2:35 PM T.J. Usiyan via swift-evolution < swift-evolution@swift.org> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub
Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose
members are strict subsets of explicitly defined enums.

Swift-evolution thread: Discussion thread topic for that proposal
<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160530/020025.html&gt;
Motivation

Consider a situation where we have an enum Color which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum LCDColorModel with only
three colors, red, blue, green .

enum Color {

case red, orange, yellow, green, blue, indigo, violet
    â€Ś
}

enum LCDColor {
    case red, green, blue
}

The cases in LCDColor in our scenario do not require different behavior
from their similarly named cases in Color. We would like, simply stated,
to explicitly restrict the cases allowed within a specific portion of our
software. There are, currently, a few approaches
1. Duplicate functionality in LCDColor

- Completely manually
- Protocols with ‘minimal’ manual duplication

2. Avoid duplication by allowing conversion to Color.

Neither of these solutions make the subset relationship between Color and
LCDColor clear or strict.
Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColor = Color.(red|green|blue)

LCDColor has all of the type and instance methods of Color.

   - Barring any technical reason, the ‘actual’ name of the type, in this
   example, is Color.(red|green|blue) This makes the relationship to Colorsyntactically
   clear. If a typealias is not desired, Color.(red|green|blue) should
   refer to the same type as LCDColor
   - Switching over Color.(red|green|blue) should only need to be
   exhaustive for the three cases .red, .green, and .blue.
   - Two initializers should be implicitly created
      - Color to LCDColor?
         - returns nil for all cases not in LCDColor
      - LCDColor to Color
         - Obvious and trivial implementation mapping cases from LCDColor
          to Color
      - Casting should be allowed
      - from superset to subset only using as? or as! syntax.
      - from subset to superset using as
   - Creating subsets of subsets is not allowed but reasonable
   conversions among subsets should be allowed if technically feasible such
   that:
      - Given subsets of C A and B, where A is a superset of B, the
      casting relationship between A and B should be similar to that
      between C and either of the other two named subsets.

Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.

If a mangled name approach is taken, the ordering of cases should be
sorted to ensure stability.
Alternatives considered

   - Do nothing. This feature is not strictly necessary but does allow
   for expressivity not currently available in the language.
   - implicitly create properties which convert to superset type.

Impact on existing code

This is an additive change which should have no breaking change to
existing code.

On Fri, Jun 3, 2016 at 4:57 PM, Austin Zheng <austinzheng@gmail.com> > wrote:

I really like the idea behind this proposal.

Some questions:

- Would the enum 'slice' be a distinct type relative to the base enum?
- On a related note, would shared cases between the sliced enum and the
base enum be implicitly convertible?
- If they aren't implicitly convertible between each other, would there
be an affordance to perform conversions (e.g. a "parentEnumType" property
and an "init?(parentEnumType:)" initializer)/
- Would you be able to further slice a sliced enum? If so, would they
share the same parent, or would the 'parent' of the sliced sliced enum be
the sliced enum?
- If the parent enum has members that switch on 'self', would those
members be available to the child automatically?
- What happens if you have two (or more) slices with disjoint members?
(e.g. 'LCDColors' and 'TrafficLightColors') Would they be considered
completely separate 'sub-types'?

Best,
Austin

On Fri, Jun 3, 2016 at 6:22 AM, T.J. Usiyan via swift-evolution < >> swift-evolution@swift.org> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc
enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums
whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](
http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum `LCDColorModel` with only
three colors, `red, blue, green` .

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

enum LCDColors {
case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different
behavior from their similarly named cases in `Colors`. We would like,
simply stated, to explicitly restrict the cases allowed within a specific
portion of our software. There are, currently, a few approaches
1. Duplicate functionality in `LCDColors`
- Completely manually
- Protocols with 'minimal' manual duplication
2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors`
and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases
must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to
existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

We have frequently discussed introducing subtype relationships between structs and enums, in an effort to allow limited implicit promotions (e.g. from small integers to wider integers). Wouldn’t that be a more general solution to this same problem?

-Chris

¡¡¡

On Jun 3, 2016, at 2:35 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub

1 Like

My only hesitation with using `enum LCDColors : Colors` is that that syntax
usually signals extending a type and I am not sure whether I am advocating
for the ability to add something to `LCDColors` without adding it to
`Colors`. I envisioned it as 'simply a subset of cases' but that is
probably just limited vision on my part.

Using the subtype syntax works for the most part, so I could certainly live
with it.

¡¡¡

On Fri, Jun 3, 2016 at 3:56 PM, Christopher Kornher <ckornher@me.com> wrote:

Your original syntax makes LCDColors clearly a type. The typealias
obscures the fact that a new type is being created. An alternative might be
something like:

enum LCDColors : Colors {
case red, green, blue
}

or perhaps

enum LCDColors subset: Colors {
case red, green, blue
}

This would would be compatible with something that I have been hoping for,
creating superset enums from multiple enums for things like errors:

enum AppErrors  {
// Defined by its subsets
}

enum NetworkingErrors  subset: AppErrors {
case NWError1 = 1000, NWError2. NWError3
}

enum UserInputErrors  subset: AppErrors {
case UIError1 = 2000, UIError2. UIError3
}

The compiler would have to check for rawValue collisions.

having the superset enum define values that could be used in children
would allow:

enum AppErrors  {
// Defined by its subsets
case NetworkingErrorBase = 1000
case UIErrorBase = 2000
}

enum NetworkingErrors  subset: AppErrors {
case NWError1 = NetworkingErrorBase, NWError2. NWError3
}

enum UserInputErrors  subset: AppErrors {
case UIError1 = UIErrorBase, UIError2. UIError3
}

On Jun 3, 2016, at 10:39 AM, T.J. Usiyan via swift-evolution < > swift-evolution@swift.org> wrote:

I don't have a problem with something as explicit as that. I was mostly
avoiding adding keywords with my proposed syntax. I have basically no tie
to the proposed syntax.

TJ

On Fri, Jun 3, 2016 at 12:32 PM, Christopher Kornher <ckornher@me.com> > wrote:

This could be useful in categorizing and grouping within large enums
ErrorType enums come to mind. Would there be any problem with making the
subset more explicit? e.g.

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

extension Colors {
subset  LCDColors  red, green, blue
}

On Jun 3, 2016, at 7:22 AM, T.J. Usiyan via swift-evolution < >> swift-evolution@swift.org> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc
enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose
members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](
http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the
entire set of colors relevant to your application with many salient methods
and operations. We have also declared an enum `LCDColorModel` with only
three colors, `red, blue, green` .

enum Colors {
case red, orange, yellow, green, blue, indigo, violet
…
}

enum LCDColors {
case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different
behavior from their similarly named cases in `Colors`. We would like,
simply stated, to explicitly restrict the cases allowed within a specific
portion of our software. There are, currently, a few approaches
1. Duplicate functionality in `LCDColors`
- Completely manually
- Protocols with 'minimal' manual duplication
2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors`
and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases
must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name
mangling be used which, along with the declaration order restriction should
mean that all possible subsets have a stable and predictable name which
contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to
existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

It seems like it would make sense to model enum subsets as a subtype relationship.

Is the core team planning on drawing up a structs/enums subtyping proposal later this year?

Austin

¡¡¡

On Jun 3, 2016, at 10:25 PM, Chris Lattner <clattner@apple.com> wrote:

On Jun 3, 2016, at 2:35 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub

We have frequently discussed introducing subtype relationships between structs and enums, in an effort to allow limited implicit promotions (e.g. from small integers to wider integers). Wouldn’t that be a more general solution to this same problem?

-Chris

1 Like

It could be a more general solution. I am unclear about what 'subtype
relationships' means here though.

Are you talking a about what you allude to here?
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151130/000525.html

The benefit of explicitly narrowing, in my opinion, is that there is no
unnecessary cost to figuring out lookup. (Please correct me if I am
mistaken.) Implicit promotions introduce uncertainty with regard to what a
value is being treated as in any given moment. This uncertainty is worth it
in many cases but I will suggest that it is not worth it when trying to
deal with a narrower set of cases from an already established set. For
example, in the graph/lattice situation, conversions must be written
because there is no reasonable conversion that can be assumed. In this
proposal, the conversion is obvious and trivial because the relationship is
completely clear.

All of that said, I *am* unclear about what subtype relationships means so
it may very well be a better solution. It certainly sounds like a more
general solution but I am not convinced that that is an advantage when
trying to deal with a strict subset.

TJ

¡¡¡

On Sat, Jun 4, 2016 at 1:25 AM, Chris Lattner <clattner@apple.com> wrote:

On Jun 3, 2016, at 2:35 PM, T.J. Usiyan via swift-evolution < > swift-evolution@swift.org> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub

We have frequently discussed introducing subtype relationships between
structs and enums, in an effort to allow limited implicit promotions (e.g.
from small integers to wider integers). Wouldn’t that be a more general
solution to this same problem?

-Chris

The syntax I described here has a number of problems, among them being:
  1) Creating a supersets in the way described breaks down badly when the declarations exist within multiple modules.
  2) The empty enum declaration is currently illegal and probably should be
  3) The superset declaration contains no clue as to what it really is.
  4) Possibly the most commonly needed use case is not possible with this syntax: Extending enums defined in other modules

I believe that best solution is explicit ```subset:``` and ‘’’superset:``` relationships. They could be added to the language at the same time, or not, but it seems to make sense to consider the possibility of creating supersets along with creating subsets.

I will take more time to think this through before I reply again. It would be nice not to add a variation to ```:``` but that is the best syntax I could think of at the time.

My only hesitation with using `enum LCDColors : Colors` is that that syntax usually signals extending a type and I am not sure whether I am advocating for the ability to add something to `LCDColors` without adding it to `Colors`. I envisioned it as 'simply a subset of cases' but that is probably just limited vision on my part.

I agree.

¡¡¡

On Jun 3, 2016, at 2:18 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

Using the subtype syntax works for the most part, so I could certainly live with it.

On Fri, Jun 3, 2016 at 3:56 PM, Christopher Kornher <ckornher@me.com <mailto:ckornher@me.com>> wrote:
Your original syntax makes LCDColors clearly a type. The typealias obscures the fact that a new type is being created. An alternative might be something like:

enum LCDColors : Colors {
	case red, green, blue
}

or perhaps

enum LCDColors subset: Colors {
	case red, green, blue
}

This would would be compatible with something that I have been hoping for, creating superset enums from multiple enums for things like errors:

enum AppErrors  {
	// Defined by its subsets
}

enum NetworkingErrors  subset: AppErrors {
	case NWError1 = 1000, NWError2. NWError3
}

enum UserInputErrors  subset: AppErrors {
	case UIError1 = 2000, UIError2. UIError3
}

The compiler would have to check for rawValue collisions.

having the superset enum define values that could be used in children would allow:

enum AppErrors  {
	// Defined by its subsets
	case NetworkingErrorBase = 1000
	case UIErrorBase = 2000
}

enum NetworkingErrors  subset: AppErrors {
	case NWError1 = NetworkingErrorBase, NWError2. NWError3
}

enum UserInputErrors  subset: AppErrors {
	case UIError1 = UIErrorBase, UIError2. UIError3
}

On Jun 3, 2016, at 10:39 AM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

I don't have a problem with something as explicit as that. I was mostly avoiding adding keywords with my proposed syntax. I have basically no tie to the proposed syntax.

TJ

On Fri, Jun 3, 2016 at 12:32 PM, Christopher Kornher <ckornher@me.com <mailto:ckornher@me.com>> wrote:
This could be useful in categorizing and grouping within large enums ErrorType enums come to mind. Would there be any problem with making the subset more explicit? e.g.

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

extension Colors {
	subset  LCDColors  red, green, blue
}

On Jun 3, 2016, at 7:22 AM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

This is loosely related to but not meant to 'compete' with the ad hoc enum proposal.

## Introduction

This proposal adds/creates syntax to allow ad hoc creation of enums whose members are strict subsets of explicitly defined enums.

Swift-evolution thread: [Discussion thread topic for that proposal](http://news.gmane.org/gmane.comp.lang.swift.evolution\)

## Motivation
Consider a situation where we have an enum `Colors` which represents the entire set of colors relevant to your application with many salient methods and operations. We have also declared an enum `LCDColorModel` with only three colors, `red, blue, green` .

enum Colors {
	case red, orange, yellow, green, blue, indigo, violet
	…
}

enum LCDColors {
	case red, green, blue
}

The cases in `LCDColors` in our scenario do not require different behavior from their similarly named cases in `Colors`. We would like, simply stated, to explicitly restrict the cases allowed within a specific portion of our software. There are, currently, a few approaches
  1. Duplicate functionality in `LCDColors`
    - Completely manually
    - Protocols with 'minimal' manual duplication
  2. Avoid duplication by allowing conversion to `Colors`.

Neither of these solutions make the subset relationship between `Colors` and `LCDColors` clear or strict.

## Proposed solution

Add syntax to describe a restricted set of cases from an enum.

typealias LCDColors = Colors.(red|green|blue)

`LCDColors ` has all of the type and instance methods of `Colors`. Cases must appear in the same order as their original declaration.

## Detailed design

While I am unsure of the entirety of the design, I propose that name mangling be used which, along with the declaration order restriction should mean that all possible subsets have a stable and predictable name which contains all of the information necessary to infer cases.

## Impact on existing code

This is an additive change which should have no breaking change to existing code.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

It seems like it would make sense to model enum subsets as a subtype relationship.

Is the core team planning on drawing up a structs/enums subtyping proposal later this year?

+1. Subtype relationship is mostly what is desired here.

Some people have also talked about members of the supertype also being available on the subtype which would involve implementation inheritance. That could probably be made to work for enums if the subtype receives identical storage and as long as all supertype members are considered final (because value type members are always final). But it's a slippery slope and would likely bloat the storage of many subtype enums.

-Matthew

¡¡¡

Sent from my iPad

On Jun 4, 2016, at 12:34 AM, Austin Zheng via swift-evolution <swift-evolution@swift.org> wrote:

Austin

On Jun 3, 2016, at 10:25 PM, Chris Lattner <clattner@apple.com> wrote:

On Jun 3, 2016, at 2:35 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub

We have frequently discussed introducing subtype relationships between structs and enums, in an effort to allow limited implicit promotions (e.g. from small integers to wider integers). Wouldn’t that be a more general solution to this same problem?

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I would *love* to get it in Swift 3.x or Swift 4, but we’ll have to see. Generics features needed by the standard library will be higher priority, for example.

-Chris

¡¡¡

On Jun 3, 2016, at 10:34 PM, Austin Zheng <austinzheng@gmail.com> wrote:

It seems like it would make sense to model enum subsets as a subtype relationship.

Is the core team planning on drawing up a structs/enums subtyping proposal later this year?

Sub typing is the answer that we're going with then?

¡¡¡

On Sat, Jun 4, 2016 at 4:26 PM, T.J. Usiyan <griotspeak@gmail.com> wrote:

It could be a more general solution. I am unclear about what 'subtype
relationships' means here though.

Are you talking a about what you allude to here?
[swift-evolution] Proposal: Auto-convert for numbers when safe

The benefit of explicitly narrowing, in my opinion, is that there is no
unnecessary cost to figuring out lookup. (Please correct me if I am
mistaken.) Implicit promotions introduce uncertainty with regard to what a
value is being treated as in any given moment. This uncertainty is worth it
in many cases but I will suggest that it is not worth it when trying to
deal with a narrower set of cases from an already established set. For
example, in the graph/lattice situation, conversions must be written
because there is no reasonable conversion that can be assumed. In this
proposal, the conversion is obvious and trivial because the relationship is
completely clear.

All of that said, I *am* unclear about what subtype relationships means so
it may very well be a better solution. It certainly sounds like a more
general solution but I am not convinced that that is an advantage when
trying to deal with a strict subset.

TJ
On Sat, Jun 4, 2016 at 1:25 AM, Chris Lattner <clattner@apple.com> wrote:

On Jun 3, 2016, at 2:35 PM, T.J. Usiyan via swift-evolution < >> swift-evolution@swift.org> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub

We have frequently discussed introducing subtype relationships between
structs and enums, in an effort to allow limited implicit promotions (e.g.
from small integers to wider integers). Wouldn’t that be a more general
solution to this same problem?

-Chris

Sub typing is the answer that we're going with then?

Most likely. At this point, we’re not really accepting additive changes to Swift 3 anyway, so any proposal would need to wait until post-swift 3 in any case.

Plans for releases beyond Swift 3 haven’t been made yet - the metaplan is to come up with a plan in ~August timeframe.

-Chris

¡¡¡

On Jun 5, 2016, at 8:24 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org> wrote:

On Sat, Jun 4, 2016 at 4:26 PM, T.J. Usiyan <griotspeak@gmail.com <mailto:griotspeak@gmail.com>> wrote:
It could be a more general solution. I am unclear about what 'subtype relationships' means here though.

Are you talking a about what you allude to here? [swift-evolution] Proposal: Auto-convert for numbers when safe

The benefit of explicitly narrowing, in my opinion, is that there is no unnecessary cost to figuring out lookup. (Please correct me if I am mistaken.) Implicit promotions introduce uncertainty with regard to what a value is being treated as in any given moment. This uncertainty is worth it in many cases but I will suggest that it is not worth it when trying to deal with a narrower set of cases from an already established set. For example, in the graph/lattice situation, conversions must be written because there is no reasonable conversion that can be assumed. In this proposal, the conversion is obvious and trivial because the relationship is completely clear.

All of that said, I *am* unclear about what subtype relationships means so it may very well be a better solution. It certainly sounds like a more general solution but I am not convinced that that is an advantage when trying to deal with a strict subset.

TJ
On Sat, Jun 4, 2016 at 1:25 AM, Chris Lattner <clattner@apple.com <mailto:clattner@apple.com>> wrote:

On Jun 3, 2016, at 2:35 PM, T.J. Usiyan via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Since this seems to have some interest, I've made a gist.

Enum Subsets ¡ GitHub

We have frequently discussed introducing subtype relationships between structs and enums, in an effort to allow limited implicit promotions (e.g. from small integers to wider integers). Wouldn’t that be a more general solution to this same problem?

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I would love to see the idea of subset resurface.

1 Like

same