-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Pass slices to functions in exercise 9.6.1 (#2848) #2850
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
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Have people learned about slices at this point? |
Ah, yes, I see they have 😄 Slices are introduced in 9.3 Slices. However, the exercise text says:
So that is the reason why the functions use the When I've been teaching the class (both inside and outside of Google), I'm sometimes met with questions about the math-related exercises. That is, while I would suggest keeping the solution as-is, but add a speaker note showing that one could use slices instead. The speaker note should then discuss the tradeoffs: with slices, you have a runtime length check, but with borrowed arrays, you have compile-time knowledge of the length (and the compiler can unroll the loops accordingly). Basically, I don't think it's 100% clear that always using slices is the right call. A vector-library would want to use const generics instead, I believe. |
That's a good point, @mgeisler -- thank you! I had missed that the instructions specifically mention arrays. This solution already has a speaker note, so adding another regarding slices seems like a good idea (and this is the segment to do it in, since we have discussed both references and slices). |
@djmitche, Can we ask @mgeisler to add a speaker note? We could mention that declaring slice
The same holds for string slices as parameters. |
Fair point - I made #2851 for that purpose. The Deref trait is covered (indirectly) later in the course, so I think it's best to omit deref coercion here. |
Thank you, @djmitche ! |
Fixes issue #2848:
Use the references to a Dynamically Sized Slice instead of references to a Fixed-Size Array in function parameters.