Hi Slow week again :( but I got this one function done. I haven't changed the return type but I'm sure it's not right. It's given as void|object but I can't see how it would ever not return anything. If the video device is uninitialized, Pike returns 0 (the original C function returns NULL). Wouldn't that be a special type of (null) object rather than void? Thanks & regards, larcky.
On Sun, Jan 30, 2011 at 02:32:54PM +0000, Matthew Clarke wrote:
I haven't changed the return type but I'm sure it's not right. It's given as void|object but I can't see how it would ever not return anything. If the video device is uninitialized, Pike returns 0 (the original C function returns NULL). Wouldn't that be a special type of (null) object rather than void?
that is what void is. if a function returns void, and you assign that, the result is 0. there is no null object in pike, generally 0 is used for that purpose. (and it works reasonaly well, except when you are dealing with integers)
greetings, martin.
Martin Bähr wrote:
that is what void is. if a function returns void, and you assign that, the result is 0. there is no null object in pike, generally 0 is used for that purpose. (and it works reasonaly well, except when you are dealing with integers)
greetings, martin.
Ahaaa... I knew you could assign 0 to just about everything but didn't realize that included 'void' as well. So you could look at void as a type that can only take one value (0), and then use it to document that a function returns 0 if it can't return its other main type (except, as you say, for integers)? Thanks =)
On Sun, Jan 30, 2011 at 10:13:37AM -0800, larcky wrote:
Ahaaa... I knew you could assign 0 to just about everything but didn't realize that included 'void' as well.
it is the other way around. if a function returns nothing, but there is an asignment (which means a value is required) than that is converted to 0.
So you could look at void as a type that can only take one value (0), and then use it to document that a function returns 0 if it can't return its other main type (except, as you say, for integers)?
void can not take any value. if you declare a function as void, returning 0 will be an error.
void fail() { return 0; // this should give an error }
void fail2() { return; }
if you assign that to somewhere:
string bar = "gazong"; bar = fail2();
it fails with: Compiler Error: 1: Assigning a void expression. because the compiler already catches this.
but in your case you have: void|string pass2() { return; }
bar = pass2();
which is successfull, because the compiler doesn't know yet if you are going to return something.
at runtime this assignment doesn't fail, but instead the missing value is replaced with a 0.
if you run fail(); or fail2(); in hilfe (the interactive pike prompt) you get: Compiler Warning: 1: Returning a void expression. Converted to zero.
greetings, martin.
Hi Martin Thanks for your reply. So a return type of 'void|object' means this function may return either an object or nothing at all. But if it returns nothing there won't be an error, because the missing value is converted to 0? Whereas if the return type were just 'object' and you returned nothing, the compiler would complain? Hope I've got that right, because that does make a lot more sense :)
Though in that case, surely there's no point to SDL.get_video_info() returning type 'void|object', because there's never going to be a scenario where it doesn't return anything. It's always going to return either an object or 0, both of which you can assign to an object and which you'd test in exactly the same way:
if ( object o = SDL.get_video_info() ) { // success }
Best wishes, larcky.
So a return type of 'void|object' means this function may return either an object or nothing at all. But if it returns nothing there won't be an error, because the missing value is converted to 0? Whereas if the return type were just 'object' and you returned nothing, the compiler would complain? Hope I've got that right, because that does make a lot more sense :)
That is correct. It would make no difference to the caller; the function could still be returning 0. But with a return type of just "object", the function needs to use "return 0;" to achieve this, rather than just "return;".
The difference is rather cosmetic; the caller has no way of knowing the function did "return;" or "return 0;", the value will be 0 in either case.
Though in that case, surely there's no point to SDL.get_video_info() returning type 'void|object', [...]
For functions implemented in C, it makes no practical difference whatsoever. So I guess the point (if any) is purely documentational. I don't know about this particular function, but if you have a function which conceptually, depending on what parameters you give it, either returns something or doesn't, then using this kind of return type could make sense, even though in reality the function will always return something. If the function conceptually returns either "an object" or "no object", as opposed to either "an object" or "nothing", then "object" is the better return type, since (object)0 is fine for "no object".
pike-devel@lists.lysator.liu.se