Reducer.optional assert with more info

I had to fix a couple of issues related to this today and when you have a bunch spread across the code is not that easy to know which one is the one trapping.

@mbrandonw would you consider adding more information into the optional assertion?

I quickly prototyped this:

extension Reducer {    
    public func optional(_ file: String = #file, _ line: Int = #line) -> Reducer<State?, Action, Environment> {
      .init { state, action, environment in
        if state == nil {
            print("Nil optional reducer: \(file):\(line)")
        return, action, environment)

and it's already way more useful:

Nil optional reducer: .../Main/Main.swift:76
Fatal error:  ....

Doing it in the library directly would be nicer since the message would be integrated in the assert. But I didn't want to open a PR without consulting since I'm afraid overloading the property with a function may cause some trouble.

What do you think?

I think this would be a nice ergonomic improvement. We'd definitely take a PR if you have a chance to work on it! We can add it to all of the higher-order reducers that can fail and pass the file/line along to assertionFailure.

I will try to make some time for it today. Thanks ;)

Terms of Service

Privacy Policy

Cookie Policy