swiftwasm / JavaScriptKit

Swift framework to interact with JavaScript through WebAssembly.
https://swiftpackageindex.com/swiftwasm/JavaScriptKit/main/documentation/javascriptkit
MIT License
664 stars 44 forks source link

Fix Optional implementation for ConstructibleFromJSValue #238

Closed omochi closed 5 months ago

omochi commented 5 months ago

The current Optional.construct returns nil when the given JS value is null or undefined, butnilmeans a failure during decode here, so it's semantically wrong. Instead, it should return.some(nil)`.

Also, when Wrapped.construct returns nil, it means decode operation failed, so Optional.construct method should return nil. But it might be unclear that the returned Optional<Wrapped> value won't be implicitly casted to .some(nil), so I changed to return nil by guard statement explicitly.

Original description 現在の実装ではJSの値として `null`, `undefined` の場合に、 `Optional.construct` が `nil` を返していますが、 これはデコードの失敗を意味するため間違っています。 適切に `Optional.none` がデコードできた状況なので、 `.some(nil)` を返す必要があります。 また、 `Wrapped.construct` が `nil` を返した場合は、 デコード失敗なので `nil` を返すべきですが、 ここで暗黙のキャストによって `.some(nil)` が返ってしまうかどうか分かりづらいと思ったので、 明示的に guard 文で分岐するように書き換えてみました。