-
Notifications
You must be signed in to change notification settings - Fork 22
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
UiaOperationScope.ForEach fetches the array element as const #69
Comments
the change would be as follows:
|
I'm also happy to accept an answer such that it is recommended that all Uia* types should always be accepted by value in functions, but in this case I'd appreciate a clear explanation as to why. |
Hmm, thanks for the feedback! Honestly, my guess as to why we made that const is just that we're getting the array item by value from I think I agree with you that it shouldn't be const. That way we allow @pengyuanjin @mavangur @zhayuli Do you have any particular thoughts on this? |
@michaelDCurran I'd say go ahead and put up a PR for this if you want (along with some tests validating multiple constness-es). Otherwise we'll get to it when we have the chance. |
UiaOperationScope has a useful utility method: ForEach which executes the given lambda on each element in the given array, whether running locally or remotely.
However, this method fetches each element from the array and passes it to the visitor function as const, which means it is impossible to modify the element from within the visitor function if the visitor function accepts the element by reference.
And because none of the Uia types have methods marked as const, it is therefore impossible to do much at all in the visitor function. For example,
Results in an error like the following:
A workaround is to have the lambda accept the element by value rather than reference:
As I believe that copying the Uia* types is remotely cheap and ends up using the same underlying object anyway.
But personally I do prefer to code my functions taking Uia* types by reference as it just feels more symantically correct, and removes any guessing about whether copying is in deed cheep or not.
I'm also really not sure why scope.forEach deliberately fetches the element from the array as a const variable? I don't think the concept of const really makes any sense for these types, especially when none of the methods on them are marked as const.
Removing const here would be a one line change. I can open a pr to do this, but I first wanted to better understand why it was made const in the first place.
The text was updated successfully, but these errors were encountered: