[swift-evolution] [Pitch] Non-class type requirements on protocols (eg : struct, : enum)

Tony Allevato allevato at google.com
Fri Oct 21 12:23:22 CDT 2016


I'd like to spin this a slightly different way.

While they're not exactly the same as structs because the compiler has
separate logic for exhaustiveness checking, enum cases have some
interesting properties that make them relatable:

* A case without a payload is like a static read-only property on the enum
type that is equal to a value of that type.
* A case with a payload is like a static function on the enum type that
creates and returns a value of that type.

The second point is interesting because if you have:

```
enum Foo {
  case bar(Int)
}
```

and you reference "Foo.bar" without a payload, what you get back is a
function of type (Int) -> Foo.

So instead of casting your example as "a protocol can be enum-constrained
and must contain certain cases", what if enums could just have their
conformance fall out naturally from the bullet point observations above?

In other words, your example protocol could be:

```
protocol Result {
  associatedtype Payload
  static func success(_ p: Payload) -> Self
  static func failure(_ e: Error) -> Self
}
```

and the cases provide the conformance without having to do anything extra:

```
enum UserParseResult: Result {
  typealias Payload = User  // can be omitted if it's inferred
  case success(User)
  case failure(Error)
}
```

Now there's a caveat here worth discussing: would the intention of an
"enum-constrained protocol" be to define the *only* cases it can have, or
just the minimal set? The former would let you do some interesting generic
protocol-constrained exhaustive pattern matching. However, given that no
other use of protocols defines an *exact* set (you don't say these are the
*only* methods/properties that a conforming type can implement), I think it
would be a hard sell to apply a stronger restriction specifically to enums.
In any case, it feels to me like an enum restricted to "only these exact
cases" calls more for a generic enum rather than a protocol-based solution.


On Fri, Oct 21, 2016 at 9:39 AM T.J. Usiyan via swift-evolution <
swift-evolution at swift.org> wrote:

> I would like the ability to specify that something is an enum so that I
> could model a generic `Result` type.
>
> ```
> protocol Result : enum {
>     associatedtype Payload
>     case success(Payload)
>     case failure(Error)
> }
> ```
>
> the basic idea being that I could then, while conforming, state which
> cases in the concrete type serve as the protocol's case.  I don't have a
> great vision for the syntax of spelling this conformance so I will make
> this painfully verbose to be clear
>
> ```
> enum UserParseResult {
>     case success(User)
>     case failure(Error)
> }
>
> extension UserParseResult : Result {
>     protocol(Result) case success = UserParseResult.success
>     protocol(Result) case failure = UserParseResult.failure
> }
> ```
>
>
> The benefit of this, in my opinion, is that we could have code commonly
> used on results everywhere written once on the protocol without sacrificing
> the ability to switch with guarantees. I can see that this suggestion has
> some rough points so all I will finish by restating the problem that I want
> to solve.
>
> There is code that is fairly common to enum types that have shared
> characteristics and/or purpose. I would find it useful to have a way to
> implement shared algorithms in a generic way while retaining core features
> of enums.
>
> On Fri, Oct 21, 2016 at 11:11 AM, Mike Kasianowicz via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Just from an outside perspective, the class restriction seems to be there
> as a kludge for technical reasons... but that's neither here nor there.
>
> It is not so much to enforce a lack of identity - in the struct case, it
> would be to enforce copy-by-value semantics.  I think the strongest
> argument I've got is, say, a serialization or caching framework where you
> want to enforce that something is entirely writeable via memory pointer or
> copyable.  A value-type restriction would get us mostly there, albeit there
> would still be ways to break the contract.  However, as noted in my
> previous email, I see a lot of possibilities for enums too - in that case
> the protocol somewhat acts as 'base type' without adding the complexity of
> a base type.
>
> I listed some of my examples in my previous email - I could elaborate if
> it helps.
>
> On Fri, Oct 21, 2016 at 9:51 AM, Karl Wagner <razielim at gmail.com> wrote:
>
> IIRC, the reason we have "class" there is for the optimiser, so it can
> optimise for the protocol being satisfied by a reference-counted type.
> Classes are semantically unique from values because they have identity,
> which is also something a protocol might want to codify.
>
> There may be some optimisation gains by requiring all conformers to be
> values, but I struggle to think of why you might want to codify that a
> conformer should not have identity.
>
> Personally I don't really like this asymmetry in the language either, and
> would support changes to make these two elements more explicit. For
> example, a magic "hasIdentity" protocol which is automatically satisfied
> only by classes, and moving the optimisation guides to usage site (e.g.
> when declaring a variable of type MyProto, I could declare it of type
> AnyClass<MyProto> or AnyValue<MyProto> instead, to annotate this specific
> instance as being refcountable or not, without making such optimisation
> hints part of the MyProto definition)
>
> - Karl
>
>
> On Oct 21, 2016 at 8:39 am, <Mike Kasianowicz via swift-evolution
> <swift-evolution at swift.org>> wrote:
>
> Currently protocols can have the class constraint:
> protocol MyProtocol : class {}
>
> It would be (a) intuitive and (b) useful to allow such things as:
> protocol Model : struct {} or protocol Event : enum {}
>
> These types of restrictions can help prevent accidental anti-patterns or
> misuse of APIs.
>
> Seems simple and non-controversial... right?
>
> [Note: I'd like to see even more heavy-handed protocol restrictions in the
> future.  For example, a protocol describing an enum with a common case, or
> a struct with no reference members. Great stuff for defensively coding
> APIs.]
> _______________________________________________ swift-evolution mailing
> list swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161021/2d2d904d/attachment.html>


More information about the swift-evolution mailing list