How to return local array in C++?

I would suggest std::vector<char>: std::vector<char> recvmsg() { std::vector<char> buffer(1024); //.. return buffer; } int main() { std::vector<char> reply = recvmsg(); } And then if you ever need char* in your code, then you can use &reply[0] anytime you want. For example, void f(const char* data, size_t size) {} f(&reply[0], reply.size()); And you’re done. That means, … Read more

Why does Array.prototype.push return the new length instead of something more useful?

I understand the expectation for array.push() to return the mutated array instead of its new length. And the desire to use this syntax for chaining reasons. However, there is a built in way to do this: array.concat(). Note that concat expects to be given an array, not an item. So, remember to wrap the item(s) … Read more

How to specify multiple return types using type-hints

From the documentation class typing.Union Union type; Union[X, Y] means either X or Y. Hence the proper way to represent more than one return data type is from typing import Union def foo(client_id: str) -> Union[list,bool] But do note that typing is not enforced. Python continues to remain a dynamically-typed language. The annotation syntax has … Read more