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.