# \[Pitch\] Opaque parameter types

**URL:** <https://forums.swift.org/t/pitch-opaque-parameter-types/54914>\
**Category:** Pitches\
**Created:** [January 27, 2022, 7:38pm UTC](https://forums.swift.org/t/pitch-opaque-parameter-types/54914 "2022-01-27T19:38:56Z")\
**Posts on this page:** 5\
**Page:** 3

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [January 31, 2022, 9:54pm UTC](https://forums.swift.org/t/pitch-opaque-parameter-types/54914/42 "2022-01-31T21:54:27Z")

</div>

> [@ksluder](#):
>
> I can’t recall whether `func f<T: P>(any T)` is legal.

No, this is not legal. The operand to `any` must also be a protocol or protocol composition. From [SE-0335](https://github.com/apple/swift-evolution/blob/main/proposals/0335-existential-any.md#semantics-of-explicit-existential-types):

> Explicit `any` can only be applied to protocols and protocol compositions, or metatypes thereof; `any` cannot be applied to nominal types, structural types, type parameters, and protocol metatypes

---

<div class="post-metadata">

**Author:** ![ensan-hcl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ensan-hcl/32/15186_2.png) [@ensan-hcl](https://forums.swift.org/u/ensan-hcl)\
**Post date:** [February 1, 2022, 12:07pm UTC](https://forums.swift.org/t/pitch-opaque-parameter-types/54914/43 "2022-02-01T12:07:48Z")

</div>

Wow. Actually, I didn't expect that. How does that behave inside function? I feel it is a quite strange exception that `(some P) -> ()` is only allowed in argument position of function.

```swift
func foo(value: some Any) {
    let a = value // possible
    let b: some Any = value // possible
    let c: any Any = value // possible
}

func foo(closure: (some Any) -> ()) {
    let a = closure // possible
    let b: (some Any) -> () = closure // impossible due to the restriction?
    let c: (any Any) -> () = closure // possible
}

```

---

<div class="post-metadata">

**Author:** ![fclout](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fclout/32/36155_2.png) [@fclout](https://forums.swift.org/u/fclout)\
**Post date:** [February 2, 2022, 4:48am UTC](https://forums.swift.org/t/pitch-opaque-parameter-types/54914/44 "2022-02-02T04:48:42Z")

</div>

A reason that `some` is interesting in return type positions is that the type is decided by the callee instead of the caller. Using `View` for lack of another concrete example, wouldn't it be nice if this worked?

```swift
func passView(action: (some View) -> Void) {
    let view = VStack { HStack { ... } ... }
    action(view)
}

```

This is not something that you can do today with the regular generic syntax. This would require the closure to be generic and accept any kind of `View`. I understand that generic closures don't exist and may well never exist, but to me it would be more self-consistent to just not allow `some` in closure parameters.

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [February 4, 2022, 11:11pm UTC](https://forums.swift.org/t/pitch-opaque-parameter-types/54914/45 "2022-02-04T23:11:23Z")

</div>

> [@ksluder](#):
>
> > [@Douglas\_Gregor](#):
> >
> > the Swift type system doesn't have first-class generic functions, i.e., you cannot have a value of type `<T>(T) -> Void` : context must provide a specific binding for `T` .
> 
> Might this feature be added in the future? If so, is this proposal forward-compatible with it? (I suspect it is, but it may not be able to express all types that a syntax with explicit type parameters could express).

We could add first-class generic function values in the future. Opaque parameter types would be no better or worse than their equivalent generic functions with respect to forward compatibility, and I'm fairly certain we could handle it: we'd bind the generic arguments when we can infer them, and leave the result generic when we cannot.

> [@ensan-hcl](#):
>
> ```swift
> func foo(closure: (some Any) -> ()) {
> let a = closure // possible
> let b: (some Any) -> () = closure // impossible due to the restriction?
> let c: (any Any) -> () = closure // possible
> }
> 
> ```

`b` is ill-formed. The only thing it could really mean would be

```swift
let b2: (_) -> () = closure 

```

I don't know that we need to allow the redundant spelling here.

Note that `c` is ill-formed, because the closure can't take `any Any`, it can only take the specific type of the closure (i.e., the synthesized generic parameter).

Doug

---

<div class="post-metadata">

**Author:** ![ensan-hcl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ensan-hcl/32/15186_2.png) [@ensan-hcl](https://forums.swift.org/u/ensan-hcl)\
**Post date:** [February 4, 2022, 11:18pm UTC](https://forums.swift.org/t/pitch-opaque-parameter-types/54914/46 "2022-02-04T23:18:59Z")

</div>

OK, I'm sorry in that `c` is ill-formed. But that's not my point. I'm asking that, why a type which cannot appear in let statements can appear in function parameter position. Other types containing `some` do appear in both parameter position and let statements. The behavior of `(some P) -> ()` is quite strange.

It contains another problem, please read my comment in the proposal review thread 🙇‍♂️ Maybe we should argue in the review thread, in order not to scatter the discussion.

> [@SE-0341: Opaque Parameter Declarations](https://forums.swift.org/t/se-0341-opaque-parameter-declarations/55082/11):
>
> [SE-0328](https://forums.swift.org/t/accepted-with-modifications-se-0328-structural-opaque-result-type/53789) rejected adding `some P` in 'consuming' position of closure, because at that point we were afraid that adding such a closure contradicts with this SE-0341 == 'generalized `some` syntax'. But this SE-0341 introduces the `some P` in 'consuming' position of closure in the parameter position of function. At least, please explain, why? This problem cannot be resolved if you prohibit the use of `(some P) -> ()` in parameter position. The proposed behavior of it is quite natural considering it as a sugar of generics. Prohibiting that introduces a new strange exception into Swift.

[Previous page](https://forums.swift.org/t/pitch-opaque-parameter-types/54914.md?page=2)
