tgrapperon / swift-dependencies-additions

More dependencies for `swift-dependencies`
MIT License
298 stars 39 forks source link

Stream of UserDefaults values #53

Closed atacan closed 1 year ago

atacan commented 1 year ago
tgrapperon commented 1 year ago

Hey @atacan! Thanks for the PR! I took the liberty to fix the test that wasn't really asserting anything because I think that the task was cancelled before reaching the assertion. I also fixed ephemeral behavior to match UserDefault's one. I'm currently thinking if we'd need to gather/namespace all the sequences or not. I'll give it a day or two.

tgrapperon commented 1 year ago

Hey @atacan! I slightly reworked a few implementations. If that's OK with you, we'll merge.

acosmicflamingo commented 1 year ago

That's pretty cool! Great work @atacan and @tgrapperon I'll look forward to updating to the latest version when it's made available :)

tgrapperon commented 1 year ago

I added more tests and I unified RawRepresentable streams APIs that I missed during my initial review. I've added some unlabeled type argument (like AsyncStream does) to help typing the streams. We'll merge once CI is passing. Thanks for the PR @atacan!

atacan commented 1 year ago

Thank you very much @tgrapperon ! I have a question. What is the benefit of .map { ($0 as! Int?) over .map { ($0 as? Int)

tgrapperon commented 1 year ago

This is more semantical than anything else. The value that comes from the erased stream is an Int? and not an Int. We're sure it's the right type, so we force cast. In the other case, we try to cast the value as an Int, and return nil if it fails. Of course it'd fail if you would pass Int?.none, but it also means that if for some reason some Double?.none is produced, it will simply map to nil too instead of crashing (this would be totally unexpected, so we don't want to recover and we want to spot this if it happens).

So in both cases you have a function (Any) -> Int?, but one crashes if you pass anything else than an Int? as argument, whereas the other would silently produce nil. In other words, it makes the code a little safer.