Skip to content

Wrong generic use of ObservableArray/MutableObservableArray? #700

@vietstone-ng

Description

@vietstone-ng

Hello,

To demonstrate my generic use, I write 2 pieces of code as below.

  1. This piece results in an error at the 'return' statement:
import Foundation
import Bond

protocol MyProtocolType { }

struct MyValueType: MyProtocolType { }

class MyCustomClass {
  static let singleton = MyCustomClass()
  
  private init() { }
  
  func getList() -> ObservableArray<MyProtocolType> {
    let list = MutableObservableArray([MyValueType()])
    return list.readOnlyView
  }
}
  1. This piece of code runs ok:
import Foundation
import Bond

protocol MyProtocolType { }

struct MyValueType: MyProtocolType { }

class MyCustomClass {
  static let singleton = MyCustomClass()
  
  private init() { }
  
  func getList() -> ObservableArray<MyValueType> {
    let list = MutableObservableArray([MyValueType()])
    return list.readOnlyView
  }
}

The difference is at the function's return type: ObservableArray<MyProtocolType> vs ObservableArray<MyValueType>.

I think the correct syntax for generic usage is the 1st one, but Xcode reports error with it. So I need to change to the 2nd one.

I don't understand it. Or is my syntax incorrect? If you know the reason, please point me out.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions