New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a single view to be automatically distributed #68
Conversation
Interesting, the use case you gave does make sense. I buy the idea of making these methods work with 1, and even 0 views (no-op). I didn't consider these cases when originally implementing these methods, so are you sure that they work correctly in the 1-view case for all possible parameters? I wouldn't be surprised if something broke for one or more parameters in the single view case. This would definitely be a good thing to extend the unit tests to cover. Related, some of the other methods in the I guess something else to consider is whether the asserts may be more useful in catching a real programmer error, or whether supporting the use cases like the one you mentioned is more important. |
@smileyborg Sorry for the delay! I was working on tests for this and ended up having to give some OSS love to some of my own projects. I think we should leave the asserts for those that wouldn't be able to do anything with fewer views, since those asserts aren't blocking any functionality and the programmer can just not call them if there aren't enough views. For consistency, I think it also makes sense to drop the assert to For 3.0.0, I'd absolutely make an argument for all array methods to be no-ops if there are no views or there are too few views for the constraint to be applied. I'm a big fan of fail-fast asserts, but I think the benefits of a safe method you can call without conditional logic fit Objective-C a little better. |
OK, take a look at the other methods when you get a chance and let me know. But I think I agree with the assert for |
@smileyborg Okay, updated! I didn't find any other methods for which it really made sense to lower the required number of views. The To that end, the only thing I had to change to get the existing implementations to work with a single view was a tweak to how Let me know if you'd like me to incorporate this work into the 3.0 branch instead. |
I can make a separate PR to remove the assertions for all these array methods for 3.0. I think doing so would allow library users to write less branchy layout code. |
Makes sense to me. I will get this merged in tomorrow or this weekend for sure. As far as the upcoming 3.0 release, that work is being done on master; in other words, the next release will be 3.0. So all of these changes are perfectly fine in this PR! And regarding this:
Are you referring to making these distribute methods just no-ops when called with 0 views? |
Yep! Sorry to be unclear. I didn't want to pack it onto this PR so you could merge this and then make a separate decision about that change. If you're on board, I can add those changes directly to this one. |
OK, reviewed the changes and looks great! Just committed them to master. Thanks again for taking the time to submit this! Now, just in case you're interested, regarding this:
The reason you had to make that slight change in behavior to Finally, regarding allowing the 0 view use case, I've been thinking about it and actually changed my mind here. The 1-view case is definitely great to support, but I think the 0-view case is a little murkier, especially in Objective-C where it's very easy to have |
NSArray
'sautoDistributeViewsAlongAxis:
requires 2 views, but the code works perfectly with just a single subview! Since theNSAssert
is from the initial commit, I thought perhaps this had been an early sanity check and not a specific design decision.I think it might make more sense to only require a single view (or perhaps none at all, in which case the method might no-op, monad-style) so that callers don't have to check the number of items in the array and implement different functionality in the case of a single view.
I'd be happy to add a test for the new single-view case if you're open to this patch.
One use case is a view with a small but variable number of children, such as a toolbar of buttons.