# ValueSemantic protocol

**URL:** <https://forums.swift.org/t/valuesemantic-protocol/41686>\
**Category:** Discussion\
**Created:** [November 2, 2020, 3:53am UTC](https://forums.swift.org/t/valuesemantic-protocol/41686 "2020-11-02T03:53:26Z")\
**Posts on this page:** 1\
**Showing post:** 65

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [November 5, 2020, 11:11pm UTC](https://forums.swift.org/t/valuesemantic-protocol/41686/65 "2020-11-05T23:11:39Z")

</div>

> [@anandabits](#):
>
> I’m think your definition is a great start! Thank you for putting the effort into laying it out so clearly and for including pragmatic conventions.

Thanks! I've tried and known I was failing at this so many times—it's a great relief to have finally come up with something I believe in. Hopefully it stands up to poking from the likes of @Joe_Groff.

> If the community adopts this definition and we move ahead with a proposal to bring support into the language what do you think we do with function types?

I don't think a useful concept of “value” exists for functions in Swift. Unlike `UnsafePointer`, we can't even compare two values of function type for equality. That points pretty clearly toward functions not having value semantics. Also, when there is a plausible notion of “value” that _could_ be reflected by what happens when you call the function, e.g.

```swift
var a = 1, next = { (a, a+=1).0 }, next1 = next

```

`next` and `next1` clearly don't exhibit the value-independence that would be required. You can, however, use `callAsFunction` to define value semantic types that are callable.

You mentioned bringing “support into the language.” The one obvious way I see to do that would be to introduce a `ValueSemantic` protocol whose semantic requirements match the bullet list at the top of my definition. We should definitely have that [**edit:** and implicit conformance of struct/enum/tuple aggregates of `ValueSemantic` types]. Beyond that, I'm not yet sure what kind of language features might be used to support the concept of value semantics further. I guess it _would_ be a shame if we somehow introduced a type-system representation of purity that didn't also help us prevent errors in the definition and/or use of `ValueSemantic`-conforming types.

> Would we do something similar to what @Chris_Lattner3 suggested [here](https://forums.swift.org/t/pitch-protocol-based-actor-isolation/41677/43) for `ActorSendable` ?

You mean create some refinement relationship? Yes, `ValueSemantic` should refine `ActorSendable`, as I have suggested elsewhere, because every type with value semantics has a trivial way to efficiently satisfy the `ActorSendable` requirements. I'd consider doing the same for `Codable` (the inverse of what @xwu suggested there), but:

- The way `Codable` can satisfy the `ActorSendable` requirements ain't necessarily so efficient.
- `X: ValueSemantic, Codable` would now create an ambiguous conformance.

> [@](#):
>
> Not having this conformance would break the use of `Never` as a bottom type for APIs that are constrained to `ValueSemantic`. I have written lots of generic code that uses `Never` in this way and would use `ValueSemantic` .

Yeah, `Never` should conform to every protocol, or at _least_ every one with no `init` or `static` requirements. If we want a less magical degenerate example, consider `struct X {}`. Making that conform to `ValueSemantic` is neither good nor bad, IMO, because while it has no useful notion of value, it also has no operations. As far as I'm concerned, go ahead if you want to 😉

---

_[View the full topic](https://forums.swift.org/t/valuesemantic-protocol/41686)._
