Rxswift: very weird wrong error (swift bug?)

Created on 28 Oct 2017  Â·  5Comments  Â·  Source: ReactiveX/RxSwift

Short description of the issue:

there is a weird error happening in this code. when i try to multiply an Int and a CGFloat and assign it to a CGFloat, i get an error that the binary operator * is not working there. that is a correct error. but when i do the same as the first line in a subscribe onNext: block, then instead of that error, i get a extraneous argument onNext.
correct error
correct-error
wrong error
wrong-error

Expected outcome:

i should be getting the same operator * error, whether its the first line in the block or not.

What actually happens:

i get a different error, one thats not related to the actual error.

Self contained code example that reproduces the issue:

  let i: Int = 3
  let f: CGFloat = 4.0
  var o: CGFloat
  Observable.just("test")
    .subscribe(onNext: { s in
      o = i * f
    })
    .disposed(by: DisposeBag())

RxSwift/RxCocoa/RxBlocking/RxTest version/commit

4.0.0

Platform/Environment

  • [X] iOS
  • [ ] macOS
  • [ ] tvOS
  • [ ] watchOS
  • [ ] playgrounds

How easy is to reproduce? (chances of successful reproduce after running the self contained code)

  • [X] easy, 100% repro
  • [ ] sometimes, 10%-100%
  • [ ] hard, 2% - 10%
  • [ ] extremely hard, %0 - 2%

Xcode version:

  9.0.1 (9A1004)

Installation method:

  • [ ] CocoaPods
  • [X] Carthage
  • [ ] Git submodules

I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)

  • [ ] yes (which ones)
  • [X] no

Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)

  • [X] just starting
  • [ ] I have a small code base
  • [ ] I have a significant code base

Most helpful comment

Hi @pvinis ,

This is compile time error o = i * f because Swift doesn't support automatic type conversions and so is this var o: CGFloat since compiler can't prove o is initialized in scope.

The weird error you are seeing is just a consequence of first error, so this is Swift compiler inconvenience. Yes, we've hit this often and it's irritating.

¯\_(ツ)_/¯

All 5 comments

Hi @pvinis ,

This is compile time error o = i * f because Swift doesn't support automatic type conversions and so is this var o: CGFloat since compiler can't prove o is initialized in scope.

The weird error you are seeing is just a consequence of first error, so this is Swift compiler inconvenience. Yes, we've hit this often and it's irritating.

¯\_(ツ)_/¯

i have reported it here https://bugs.swift.org/browse/SR-6246

@pvinis, it should be: o = CGFloat(i) * f and there isn't error, as well Kruno told you

well yes I understand that. it's still a swift bug. so I kept this issue closed and I just linked the issue over there for future reference and for anyone interested.

Old issue, but this thread solved it for me. basically you have add the onError: closure, and the compiler will point out the correct error message.

  let i: Int = 3
  let f: CGFloat = 4.0
  var o: CGFloat
  Observable.just("test")
    .subscribe(
      onNext: { s in o = i * f },
      onError: nil
    ).disposed(by: DisposeBag())
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gekitz picture gekitz  Â·  3Comments

apoloa picture apoloa  Â·  3Comments

RafaelPlantard picture RafaelPlantard  Â·  3Comments

Z-JaDe picture Z-JaDe  Â·  3Comments

hannesstruss picture hannesstruss  Â·  3Comments