Comments (8)
Swift's generic system is still growing. Generic covariance is limited to only the standard library types at this moment.
from reactiveswift.
You can map the value to Any?
. Custom generic types in Swift are invariant, so Signal<String, NoError>
is not a subtype of Signal<Any?, NoError>
.
But may I ask what you are trying to accomplish? You can have a composed signal Signal<(Any?, String>
of the two using combineLatest(with:)
, which is strong-typed.
from reactiveswift.
Just want to run a validate
method whenever any form field signal triggers. We have a lot of fields so I aggregate them in different methods, but I can't get the return type correct :/
func __section1Sigs() -> Signal<_, _> { // Tried func __sigs() -> Signal<Any?, NoError> {
let (s1, observer1) = Signal<String, NoError>.pipe()
let (s2, observer2) = Signal<Any?, NoError>.pipe()
return Signal.combineLatest(s1, s2)
}
Cannot convert value of type 'Signal<Any?, NoError>' (aka 'Signal<Optional<Any>, NoError>') to expected argument type 'Signal<_, _>'
from reactiveswift.
If you do s1.combineLatest(with: s2)
, or Signal.combineLatest(s1, s2)
, it gives you a signal that emits tuples, i.e. Signal<(String, Any?), NoError>
.
from reactiveswift.
I have this gross solution:
EXAMPLE
func __sigs() -> (sig: Signal<Any?, NoError>, os: [Any]) {
let (s1, o1) = Signal<String, NoError>.pipe() // In actual code I have 10+ signals in each method
let (s2, o2) = Signal<Any?, NoError>.pipe()
let (s, o) = Signal<Any?, NoError>.pipe()
Signal.combineLatest(s1, s2).observeValues{ _ in
o.send(value: "anything") // gross hack
}
return (sig: s, os: [o1, o2])
}
let a: (sig: Signal<Any?, NoError>, os: [Any]) = __sigs()
a.sig.observeValues { (things: Any?) in
print("work")
}
let o1 = a.os.first! as! Observer<String, NoError>
let o2 = a.os[1] as! Observer<Any?, NoError>
o1.send(value: "ao")
o2.send(value: "ao") // Just triggers the above to print "work"
o2.send(value: "ao")
More realistic code:
fileprivate final func __investmentOptionSigs() -> Signal<Any?, NoError> {
let s1: Signal<Any?, NoError> = specialBtn.reactive.values(forKeyPath: "isSelected").startSignal()
let s2: Signal<String, NoError> = tf2.reactive.continuousTextValues
let s3: Signal<String, NoError> = tf3.reactive.continuousTextValues
let s4: Signal<String, NoError> = tf4.reactive.continuousTextValues
let s5: Signal<String, NoError> = tf5.reactive.continuousTextValues
let s6: Signal<String, NoError> = tf6.reactive.continuousTextValues
let s7: Signal<String, NoError> = tf7.reactive.continuousTextValues
let (s, o) = Signal<Any?, NoError>.pipe()
Signal.combineLatest(s1, s2, s3, s4, s5, s6, s7).observeValues { _ in
o.send(value: "trigger")
}
return s
}
Really want to have the return be: return Signal.combineLatest(s1, s2, s3, s4, s5, s6, s7)
... I have many more than 7 signals
from reactiveswift.
Formal question: Is there a way to make the return signature match w/e Signal.combineLatest()
returns?
My initial solution was to return an array
of Signals which would then be used as Signal.combineLatest(array)
elsewhere, but the strong typing is preventing that.
from reactiveswift.
Formal question: Is there a way to make the return signature match w/e Signal.combineLatest() returns?
As said, Signal<U, NoError>
is not a subtype of Signal<Any?, NoError>
. You can map it as Any?
though, but when you retrieve it you need the full type signature.
My initial solution was to return an array of Signals which would then be used as Signal.combineLatest(array) elsewhere, but the strong typing is preventing that.
If you have such tremendous amount of state with fields that are distinct, you can always consider packing them in a struct
and take advantage of Swift's partial application.
// Struct
struct FormState {
let name: String?
let age: String?
let occupation: String?
let city: String?
// Implicit internal memberwise initializer.
}
// This gives a `Signal<(String?, String?, String?, String?), NoError>`.
Signal.combineLatest(nameField.reactive.continuousTextValues,
ageField.reactive.continuousTextValues,
occupationField.reactive.continuousTextValues,
cityField.reactive.continuousTextValues)
.map(FormState.init)
// If you prefer named tuple:
typealias FormState = (a: String?, b: String?, c: String?, d: String?)
[...].map { $0 as FormState }
from reactiveswift.
Appreciate the help @andersio! I'm going to map all values to Any?
.
Just feels weird since all objects are already Any
...
from reactiveswift.
Related Issues (20)
- Release 6.6.0 requires Swift 5.3 and therefore Xcode 12.x
- Using "<~" binding function with Signal.Observers causes memory leaks. HOT 3
- Using old xcconfig causing problems with Carthage & Apple Silicon macOS builds
- Support await / AsyncSequence HOT 13
- Playground does not work HOT 1
- Hello, ask a question about Disposable, thanks HOT 1
- What's means 'targeting' in QueueScheduler? HOT 2
- Signal.merge reported issue after migrating from ReactiveSwift from 4.0.0 -> 6.6.0 HOT 2
- xCode13 CompileSwiftSources normal x86_64 com.apple.xcode.tools.swift.compiler
- Usage with SwiftUI HOT 4
- Could not find module 'ReactiveSwift' for target 'arm64-apple-ios-simulator' HOT 2
- [BUG] XCode 14 beta 3 - bitcode issue HOT 1
- Is it planned to add count argument to `collect(every:on:skipEmpty:discardWhenCompleted:)`?
- Adopt OSAllocatedUnfairLock on iOS 16 HOT 9
- Add support for mapping Property/MutableProperty types to a Binding
- Signal triggered by phone call when device is closed HOT 3
- Infinite recursion in observeSwitchToLatest() HOT 3
- EXC_BAD_ACCESS Crashes occur in xcode15 HOT 4
- Reactive Swift crash with getCache function and observeSwitchToLatest signal
- Is there any plan for future releases? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reactiveswift.