-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Adapted ChartLegendRenderer class to upcoming Swift 3 changes and improved code readability #643
Conversation
Removing c-style loops is probably a good idea since they will be deprecated in swift 2.2 and removed in 3. Is it faster if you remove the |
That is quite a difference! I offer my apologies and will change the title. C-style loops are going away, along with ++ and -- operators, their replacement is more of future-proof thing than a performance improvement. I wrongly assumed that for-in and C-style loops had comparable performances. If there are better ways to replace them, please let me know: I will be happy to change/replace that commit. This doesn't make things much better but, at least on the first cycle, both for-in and C-style loops have similar performance when the upper limit is controlled by multiple variables (sometimes the for-in loop even beats the C-Style one) |
|
@liuxuan30 Yes, this is exactly the definition of the defer statement. |
@zntfdr thanks! another question, what's the swift 3 changes? |
@liuxuan30 sure! No problem :) As for the Swift 3 changes, please refer to these two comments. If you're interested on knowing more about the upcoming Swift (major) changes, I'm happy to invite you to visit this official Swift Evolution Repository. |
Actually I tend not to have multiple conditions if I can do a "max" or a "min" ahead of time and store in a variable. But that's a "moo point" as they will remove C style loops. Which is weird. Why would they do that? One might want to add another condition which is not on the same index... |
Adapted ChartLegendRenderer class to upcoming Swift 3 changes and improved code readability
These are the official reasons. Regarding the performance concerns, I'd invite you to read this fierce discussion that went on during the proposal review (the performance issue starts here). TL;DR: If performance is a major factor here, the best solution is testing different Swift loop styles every time one is needed and use the best accordingly (I don't believe that this is a priority at this state of the framework development). |
I feel sorry that for loop is going to be removed :( Is there a vote I can protest |
I just recall we have code like If c-style for loop is dropped in swift 3, we need to convert those. I am still surprised the reviews are not having a consistent opinion but they accept this idea. |
Yes I'm not really arguing about performance here, as that's not where an addition 0.0001 ms would make a difference. If there's a performance critical loop on that "realtime" level- I would just revert to a It seems like the main reason why I'm guessing that the behaviour of the |
I disagree. I like the change. C style for loops had to be mutating by default which isn't very swift like. You can still achieve the most of the same things with for in but you have to think more swifty. An example mentioned in the mailing list is iterating with 2 values. It translates to this: for (i, j) in zip(10.stride(to: 0, by: -1), 20.stride(to: 0, by: -2)) {
if i % 2 == 0 { continue }
print(i, j)
} This is more swift like and doesn't expose state by having to keep track of incrementing variables. I like this more personally but I hate using |
As I understand it, the point of Swift is to concentrate on functionality and not on syntax and code wizardy, to make code more readable and understandable etc. But I feel like sometimes the new Swift tricks are worse in readability terms than plain old C style. In the case of C style loops- only time will tell... Let's see what happens in 4.0 :) |
We will see indeed. I'm just happy to work with a language that I really like 😀. |
|
@liuxuan30 yes that's understandable... The people proposing such changes have the claim that "someone coming from a non-C language will have a steeper learning curve" as an excuse to do something weird or to remove C-like syntax, and come up with an even steeper learning curve. |
@danielgindi as a C developer, I completely agree. If people really want to write C-style loops, it's easy to integrate a C class inside a Swift framework. Syntactically, Swift is completely different than C, and the C-Style loop was a black sheep in this new language. |
@zntfdr I agree - as of the moment. But in the past, when Swift was founded, it was meant to be completely interoperable and compatible with C, C++ and ObjC. So I really get why C loops would be weird in Swift, but those arguments that C style implies a steeper learning curve - those are just ridiculous. A few weeks ago, I recall I stumbled upon a post in SO, where someone recommended using So coming back to Swift- if they now state that C/C++ compatibility is not a priority anymore - it's completely acceptable to remove C style code. And we might even see them removing the |
I think it's a good idea to evolve from the concepts of c. Even languages looking to replace c like rust have this kind of iteratior based for loop instead. I like the explanation the rust docs give about their for loops |
I'm sorry to bring this discussion up again: Cheers! |
Just some small changes under the hood.