Giter Site home page Giter Site logo

Comments (8)

andersio avatar andersio commented on June 18, 2024 1

Swift's generic system is still growing. Generic covariance is limited to only the standard library types at this moment.

from reactiveswift.

andersio avatar andersio commented on June 18, 2024

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.

MaximusMcCann avatar MaximusMcCann commented on June 18, 2024

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.

andersio avatar andersio commented on June 18, 2024

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.

MaximusMcCann avatar MaximusMcCann commented on June 18, 2024

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.

MaximusMcCann avatar MaximusMcCann commented on June 18, 2024

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.

andersio avatar andersio commented on June 18, 2024

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.

MaximusMcCann avatar MaximusMcCann commented on June 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.