# \[Accepted\] SE-0244: Opaque Result Types

**URL:** <https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294>\
**Category:** Announcements\
**Created:** [April 18, 2019, 3:34am UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294 "2019-04-18T03:34:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [April 18, 2019, 3:34am UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/1 "2019-04-18T03:34:35Z")

</div>

SE-0244 has been accepted.

> [@SE-0244: Opaque Result Types (reopened)](https://forums.swift.org/t/se-0244-opaque-result-types-reopened/22942/57):
>
> Proposal Accepted The core team agrees that this proposal lays important groundwork that can be built on further in ways laid out in the author's [Generics UI Improvements overview](https://forums.swift.org/t/improving-the-ui-of-generics/). This write-up helped clarify the context of the proposal both for reviewers and the core team itself. Several reviewers felt that being able to name opaque types is an important feature for many use cases. This is clearly a useful feature and would be a good next step, but the core team thinks that the feature as pro…

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [April 26, 2019, 9:41am UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/2 "2019-04-26T09:41:19Z")

</div>

If a function is `open` or `public`, I guess it needs the type after the “`some`“ to be public too. What about the actual return type; since the actual type is hidden only at the user level (and not to the linker), doesn’t the actual type need to be public too?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [April 26, 2019, 3:45pm UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/3 "2019-04-26T15:45:08Z")

</div>

It's hidden from the linker as well. There's a run-time call to fetch the type.

---

<div class="post-metadata">

**Author:** ![omochimetaru](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/omochimetaru/32/9598_2.png) [@omochimetaru](https://forums.swift.org/u/omochimetaru)\
**Post date:** [May 17, 2019, 9:24am UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/4 "2019-05-17T09:24:27Z")

</div>

In spite of that proposal status is `Status: Implemented (Swift 5.1)`,  
some codes appeared in proposal document are still can not be compiled.  
Is this actually working on implementation against "Status" now?

I found these cases.

```swift
[omochi@omochi-iMac-PC43 check]$ cat c1.swift
let strings: some Collection = ["hello", "world"]
[omochi@omochi-iMac-PC43 check]$ swift c1.swift
c1.swift:1:32: error: cannot convert value of type '[String]' to specified type 'some Collection'
let strings: some Collection = ["hello", "world"]
                               ^ ~~~~~~~~~~~~~~~~~
                                                  as! some Collection

```

```swift
[omochi@omochi-iMac-PC43 check]$ cat c2.swift
public protocol P {
    mutating func flip()
}

private struct Witness: P {
    mutating func flip() { /* ... */ }
}

public var someP: some P = Witness()
[omochi@omochi-iMac-PC43 check]$ swift c2.swift
c2.swift:9:28: error: cannot convert value of type 'Witness' to specified type 'some P'
public var someP: some P = Witness()
                           ^ ~~~~~~~~
                                     as! some P

```

```swift
[omochi@omochi-iMac-PC43 check]$ cat c3.swift
public protocol P { }
private struct Impl: P { }

public struct Vendor {
    private var storage: [Impl] = [Impl()]
    
    public var count: Int {
        return storage.count
    }
    
    public subscript(index: Int) -> some P {
        get {
            return storage[index]
        }
        set (newValue) {
            storage[index] = newValue
        }
    }
}

var vendor = Vendor()
vendor[0] = vendor[2] // ok: can move elements around
[omochi@omochi-iMac-PC43 check]$ swift c3.swift
c3.swift:16:30: error: cannot assign value of type 'some P' to type 'Impl'
            storage[index] = newValue
                             ^ ~~~~~~~
                                      as! Impl

```

I tested with `swift-DEVELOPMENT-SNAPSHOT-2019-05-15-a`.

---

<div class="post-metadata">

**Author:** ![jechris](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@jechris](https://forums.swift.org/u/jechris)\
**Post date:** [May 17, 2019, 12:15pm UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/5 "2019-05-17T12:15:57Z")

</div>

Similarly following example from proposal works while it shouldn't (according to the doc):

```swift
func foo() -> some BinaryInteger { return 219 }
var x = foo()
x = 912 // error: Int is not known to be the same as the return type as foo()
print(x)

```

It actually compiles and print 912.

(tested against 5.1-DEVELOPMENT-SNAPSHOT-2019-05-09-a)

---

<div class="post-metadata">

**Author:** ![DeFrenZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/defrenz/32/827_2.png) [@DeFrenZ](https://forums.swift.org/u/DeFrenZ)\
**Post date:** [May 17, 2019, 1:26pm UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/6 "2019-05-17T13:26:45Z")

</div>

> [@jechris](#):
>
> Similarly following example from proposal works while it shouldn't (according to the doc):

`BinaryInteger` conforms to `ExpressibleByIntegerLiteral`, so whatever that opaque type is this can still work

---

<div class="post-metadata">

**Author:** ![omochimetaru](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/omochimetaru/32/9598_2.png) [@omochimetaru](https://forums.swift.org/u/omochimetaru)\
**Post date:** [May 17, 2019, 2:26pm UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/7 "2019-05-17T14:26:05Z")

</div>

I didn't know it. It is interesting.  
Literal `912` is implicitly convertible to `returnTypeOf(foo)` since this type is `ExpressibleByIntegerLiteral`.

This code can not be compiled correctly.

```swift
func foo() -> some BinaryInteger { return 219 }
var x = foo()
x = Int(912) // cannot assign value of type 'Int' to type 'some BinaryInteger'
print(x)

```

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [May 17, 2019, 3:05pm UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/8 "2019-05-17T15:05:04Z")

</div>

Yeah, this is a bug in the proposal text rather than the implementation.

[https://github.com/apple/swift-evolution/pull/1042](https://github.com/apple/swift-evolution/pull/1042)

---

<div class="post-metadata">

**Author:** ![omochimetaru](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/omochimetaru/32/9598_2.png) [@omochimetaru](https://forums.swift.org/u/omochimetaru)\
**Post date:** [May 21, 2019, 1:48am UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/9 "2019-05-21T01:48:26Z")

</div>

I opened issue about what I wrote.

> <https://github.com/apple/swift/issues/53120>
>
> | | |
> |------------------|-----------------|…
> |Previous ID | SR-10723 |
> |Radar | None |
> |Original Reporter | @omochi |
> |Type | Bug |
> 
> \<details\>
> \<summary\>Environment\</summary\>
> 
> swift-DEVELOPMENT-SNAPSHOT-2019-05-15-a.
> 
> 
> \</details\>
> 
> \<details\>
> \<summary\>Additional Detail from JIRA\</summary\>
> 
> | | |
> |------------------|-----------------|
> |Votes | 0 |
> |Component/s | Compiler |
> |Labels | Bug, OpaqueResultTypes |
> |Assignee | None |
> |Priority | Medium |
> 
>    
> 
> md5: a0bd45a112a4032da0f956a2c79354a0
> 
> \</details\>
> 
> 
> 
> 
> 
> \*\*Issue Description:\*\*
> 
> 
> These code can not compile.
> 
> \`\`\` java
> \[omochi@omochi-iMac-PC43 check\]$ cat c1.swift
> let strings: some Collection = \["hello", "world"\]
> \[omochi@omochi-iMac-PC43 check\]$ swift c1.swift
> c1.swift:1:32: error: cannot convert value of type '\[String\]' to specified type 'some Collection'
> let strings: some Collection = \["hello", "world"\]
> ^ ~~~~~~~~~~~~~~~~~
> as! some Collection
> \`\`\`
> 
> \`\`\` java
> \[omochi@omochi-iMac-PC43 check\]$ cat c2.swift
> public protocol P {
> mutating func flip()
> }
> 
> private struct Witness: P {
> mutating func flip() { /\* ... \*/ }
> }
> 
> public var someP: some P = Witness()
> \[omochi@omochi-iMac-PC43 check\]$ swift c2.swift
> c2.swift:9:28: error: cannot convert value of type 'Witness' to specified type 'some P'
> public var someP: some P = Witness()
> ^ ~~~~~~~~
> as! some P
> \`\`\`
> 
> But both are in proposal document.  
> \<https://github.com/apple/swift-evolution/blob/master/proposals/0244-opaque-result-types.md\>

> <https://github.com/apple/swift/issues/53121>
>
> | | |
> |------------------|-----------------|…
> |Previous ID | SR-10724 |
> |Radar | None |
> |Original Reporter | @omochi |
> |Type | Bug |
> 
> \<details\>
> \<summary\>Environment\</summary\>
> 
> swift-DEVELOPMENT-SNAPSHOT-2019-05-15-a.
> 
> 
> \</details\>
> 
> \<details\>
> \<summary\>Additional Detail from JIRA\</summary\>
> 
> | | |
> |------------------|-----------------|
> |Votes | 0 |
> |Component/s | Compiler |
> |Labels | Bug, OpaqueResultTypes |
> |Assignee | None |
> |Priority | Medium |
> 
>    
> 
> md5: 4ad9b24159d34e1b30e2ef152479ad4b
> 
> \</details\>
> 
> 
> 
> 
> 
> \*\*Issue Description:\*\*
> 
> 
> \`\`\` java
> \[omochi@omochi-iMac-PC43 check\]$ cat c3.swift
> public protocol P { }
> private struct Impl: P { }
> 
> public struct Vendor {
> private var storage: \[Impl\] = \[Impl()\]
>     
> public var count: Int {
> return storage.count
> }
>     
> public subscript(index: Int) -\> some P {
> get {
> return storage\[index\]
> }
> set (newValue) {
> storage\[index\] = newValue
> }
> }
> }
> 
> var vendor = Vendor()
> vendor\[0\] = vendor\[2\] // ok: can move elements around
> \[omochi@omochi-iMac-PC43 check\]$ swift c3.swift
> c3.swift:16:30: error: cannot assign value of type 'some P' to type 'Impl'
> storage\[index\] = newValue
> ^ ~~~~~~~
> as! Impl
> \`\`\`
> 
> Setter need to force cast.  
> It is bug.
> 
> This code is in proposal document.  
> \<https://github.com/apple/swift-evolution/blob/master/proposals/0244-opaque-result-types.md\>

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [May 21, 2019, 3:30am UTC](https://forums.swift.org/t/accepted-se-0244-opaque-result-types/23294/10 "2019-05-21T03:30:22Z")

</div>

Thanks! Support for settable and stored properties isn't done yet. Get-only computed properties should work at this point.
