Changing a function's return type
Using BRONTO_INLINE()
to change a function's return type
Given a function that uses a weak type, like
int64_t GetUserId(std::string_view username);
you may want to switch this to a stronger return type.
First add a function with the new signature that you want.
int64_t GetUserId(std::string_view username);
UserId GetTypedUserId(std::string_view username) {
return UserId{GetUserId(username)};
}
Then switch the new function to be the primary implementation and the old function to call the new one.
UserId GetTypedUserId(std::string_view username);
int64_t GetUserId(std::string_view username) {
return GetTypedUserId(username).value();
}
Now annotate the old function to be inlined.
UserId GetTypedUserId(std::string_view username);
BRONTO_INLINE()
int64_t GetUserId(std::string_view username) {
return GetTypedUserId(username).value();
}
Sit back while Bronto switches all the callers to use the typed version with an explicit discard.
After Bronto has removed all calls to the original version, you can simply delete it.
UserId GetTypedUserId(std::string_view username);
This refactor is particularly powerful when combined with function renaming and expression pattern matching to strengthen the types for local variables.
See it in action on Compiler Explorer.