# Introduce an "ExpressibleByCase" protocol (to facilitate struct-based enums with payload)

**URL:** <https://forums.swift.org/t/introduce-an-expressiblebycase-protocol-to-facilitate-struct-based-enums-with-payload/40188>\
**Category:** Discussion\
**Created:** [September 11, 2020, 5:15am UTC](https://forums.swift.org/t/introduce-an-expressiblebycase-protocol-to-facilitate-struct-based-enums-with-payload/40188 "2020-09-11T05:15:34Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Charles\_Constant](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/charles_constant/32/13103_2.png) [@Charles\_Constant](https://forums.swift.org/u/Charles_Constant)\
**Post date:** [September 11, 2020, 5:28am UTC](https://forums.swift.org/t/introduce-an-expressiblebycase-protocol-to-facilitate-struct-based-enums-with-payload/40188/2 "2020-09-11T05:28:08Z")

</div>

## A couple more thoughts...

* * *

We could also provide an alternative that _isn't_ stringly-typed:

```swift
/* Swift supplies init args by the position in which they are declared */
protocol ExpressibleByCaseIndex {
    init( `case`: Int, arguments: Any ... )
}

```

Maybe that's better in terms of performance, and prevention of typos.

* * *

_Also_, my preference is to allow cases to be usable _ **with, or without their arguments** _: so we allow both .foo and .foo(123) and let the programmer decide how to handle that in init. If we want to give the programmer the option to allow/forbid this, maybe we could use a new syntax for declaring cases. Eg:

```swift
struct Param: ExpressibleByCase {
    case 
        height(Int)?, // accepts .height(123) *or* .height
        length(Int) // accepts .length(123) *only* 

```

This would be the missing piece that makes [a paradigm for accessing an enum payload](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/239) (that the introduction of @dynamicCallable made possible), ergonomic.

* * *

Everyone has different tastes, but to mine, the struct paradigm is more convenient than Swift enums-with-payload.

---

_[View the full topic](https://forums.swift.org/t/introduce-an-expressiblebycase-protocol-to-facilitate-struct-based-enums-with-payload/40188)._
