ReactiveObjC icon indicating copy to clipboard operation
ReactiveObjC copied to clipboard

Generics in `map:`

Open Pitometsu opened this issue 7 years ago • 5 comments

@erichoracek, @mdiep Can ReactiveObjC use generics for map:-like operators as well, in way described here (use additional mapper wrapper for strict type checking)? Disadvantage of that approach -- it can't be done implicitly.

Pitometsu avatar Dec 08 '16 21:12 Pitometsu

Maybe, use some macros to avoid prefix notation?

Pitometsu avatar Dec 08 '16 21:12 Pitometsu

So something like

[signal map:^OutputType(InputType obj) { ... }]

should be expanded by macro to something like

    [ ({ Mapper<InputType, OutputType> *mapper = Mapper.new; })
      map: signal
withBlock: ^OutputType(InputType obj) { ... } ]

// or even (Signal already have one generic, so Mapper may just inherit it):
//
// [ ({ Mapper<OutputType> *mapper = [Mapper mapperWithSignal:signal]; }) map:
//  ^OutputType(InputType obj) { ... } ]
//


Or use type casting instead of scopes: [ ((Mapper<InputType, OutputType> *)Mapper.new) map...

It will be even easier:

replace just

signal map:^OutputType

by

((Mapper<OutputType> *)Mapper mapperWithSignal:signal]).map:^OutputType

Pitometsu avatar Dec 08 '16 22:12 Pitometsu

However, it may broke -map: autocompletion, but compiler will check return value to match proper generic.

Pitometsu avatar Dec 08 '16 22:12 Pitometsu

I don't think it's worth the time to try to build something like this for Objective-C—especially since this concept can't be implemented cleanly. IMO you're better off spending that time trying to migrate your code to Swift.

mdiep avatar Dec 08 '16 22:12 mdiep

I can't migrate to Swift until it have backward binary compatibility. Anyway, ObjC still here, and would be nice to have type safe map: for it too.

Pitometsu avatar Dec 08 '16 23:12 Pitometsu