Skip to content
This repository has been archived by the owner on Mar 16, 2020. It is now read-only.

Signal could be struct #21

Closed
sashankg opened this issue Nov 20, 2015 · 2 comments
Closed

Signal could be struct #21

sashankg opened this issue Nov 20, 2015 · 2 comments
Labels

Comments

@sashankg
Copy link

Wouldn't it be better to have the Signal object be a struct rather than a final class. This is ensures safety when copying and is preferred in Swift (as seen in this talk) over classes. Really, I just wanted to know why it was a final class.

@JensRavens
Copy link
Owner

Signals have to be classes so they can be referenced (classes are reference types, structs are not).

let a = Signal<String>()

func doSomething(s: Signal<String>) {
  s.next { string in
    // do something important
 }
}

doSomething(a)
a.update("Hello")

If Signal would be a value type, doSomething wouldn't be able to change it (it could declare s as inout mutable, but that just breaks the whole concept of sane dependencies). Therefore the signal couldn't add the func as a subscriber and also it cannot call it back. s would never update and call their subscribers, because it would be a different instance than a.

So no, signal could not be a struct.

@sashankg
Copy link
Author

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants