Repository: expo/expo
File: packages/expo-modules-core/ios/Core/DynamicTypes/DynamicArrayType.swift (Swift)

Code before:
  }

  func equals(_ type: AnyDynamicType) -> Bool {
    if let arrayType = type as? Self {
      return arrayType.elementType.equals(elementType)
    }
    return false
  }

  func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
    let value = jsValue.getArray()
    return try value.map { try elementType.cast(jsValue: $0, appContext: appContext) }
  }

  func cast<ValueType>(_ value: ValueType, appContext: AppContext) throws -> Any {
    if let value = value as? [Any] {
      return try value.map { try elementType.cast($0, appContext: appContext) }
    }
    // We should probably throw an error if we get here. On the other side, the array type
    // requirement can be more loosen so we can try to arrayize values that are not arrays.
    return [try elementType.cast(value, appContext: appContext)]
  }

  func convertResult<ResultType>(_ result: ResultType, appContext: AppContext) throws -> Any {
    if let result = result as? [Any] {
      return result.map { Conversions.convertFunctionResult($0, appContext: appContext) }
    }
    return result
  }

  var description: String {
    "[\(elementType.description)]"
  }
}

/**
 A type-erased protocol used to recognize if the generic type is an array type.
 `Array` is a generic type, so it's impossible to check the inheritance directly.
 */
internal protocol AnyArray {
  /**
   Exposes the `Element` generic type wrapped by the dynamic type to preserve its metadata.
   */
  static func getElementDynamicType() -> AnyDynamicType
}

/**
 Extends the `Array` type to expose its generic `Element` as a dynamic type.
 */
extension Array: AnyArray {
  static func getElementDynamicType() -> AnyDynamicType {

Review comment (bug):
[💡 suggestion] Subtle behavior change worth noting: the old code used `try elementType.convertResult(element, ...)` which propagated conversion errors. `Conversions.convertFunctionResult` uses `try?` internally (line 189, 193 in Conversions.swift), so conversion errors are now silently caught and the value falls through to `convertFunctionResultInRuntime`.

In practice this shouldn't matter — `DynamicRawType.convertResult` (the previous `elementType` for `[Any]`) rarely threw — but it's worth being aware of for future debugging if a conversion silently degrades.

Code after:
  }

  func equals(_ type: AnyDynamicType) -> Bool {
    if let arrayType = type as? Self {
      return arrayType.elementType.equals(elementType)
    }
    return false
  }

  func cast(jsValue: JavaScriptValue, appContext: AppContext) throws -> Any {
    let value = jsValue.isArray() ? jsValue.getArray() : [jsValue]
    return try value.map { try elementType.cast(jsValue: $0, appContext: appContext) }
  }

  func cast<ValueType>(_ value: ValueType, appContext: AppContext) throws -> Any {
    if let value = value as? [Any] {
      return try value.map { try elementType.cast($0, appContext: appContext) }
    }
    // We should probably throw an error if we get here. On the other side, the array type
    // requirement can be more loosen so we can try to arrayize values that are not arrays.
    return [try elementType.cast(value, appContext: appContext)]
  }

  func convertResult<ResultType>(_ result: ResultType, appContext: AppContext) throws -> Any {
    if let result = result as? [Any] {
      return result.map { Conversions.convertFunctionResult($0, appContext: appContext) }
    }
    return result
  }

  var description: String {
    "[\(elementType.description)]"
  }
}

/**
 A type-erased protocol used to recognize if the generic type is an array type.
 `Array` is a generic type, so it's impossible to check the inheritance directly.
 */
internal protocol AnyArray {
  /**
   Exposes the `Element` generic type wrapped by the dynamic type to preserve its metadata.
   */
  static func getElementDynamicType() -> AnyDynamicType
}

/**
 Extends the `Array` type to expose its generic `Element` as a dynamic type.
 */
extension Array: AnyArray {
  static func getElementDynamicType() -> AnyDynamicType {