LWN.net Logo

Cracks in the Foundation (PHP Advent)

Cracks in the Foundation (PHP Advent)

Posted Dec 19, 2011 3:52 UTC (Mon) by imgx64 (guest, #78590)
In reply to: Cracks in the Foundation (PHP Advent) by HelloWorld
Parent article: Cracks in the Foundation (PHP Advent)

I'd attribute this to the lack of proper exceptions and/or multiple return values than dynamic typing. For example, the C standard library is terrible when it comes to returning errors, and it's statically-typed.

Of course it can be improved (assuming PHP has proper exceptions), but it requires a change in the culture of PHP programmers. Something I highly doubt is possible.


(Log in to post comments)

Cracks in the Foundation (PHP Advent)

Posted Dec 22, 2011 11:20 UTC (Thu) by justincormack (subscriber, #70439) [Link]

Multiple return values is the real answer here. Exceptions are probably overkill. Dont suppose php will get them though.

Cracks in the Foundation (PHP Advent)

Posted Dec 22, 2011 11:59 UTC (Thu) by etienne (subscriber, #25256) [Link]

> Multiple return values

Sometimes, in C, I would like a function to return a value (like now) *and* the (processor) flags - for instance the "zero" flag would mean no error occured. Very fast and simple...

Cracks in the Foundation (PHP Advent)

Posted Dec 23, 2011 10:09 UTC (Fri) by jezuch (subscriber, #52988) [Link]

> Sometimes, in C, I would like a function to return a value (like now) *and* the (processor) flags - for instance the "zero" flag would mean no error occured. Very fast and simple...

You mean like the errno variable?

Cracks in the Foundation (PHP Advent)

Posted Jan 3, 2012 12:41 UTC (Tue) by etienne (subscriber, #25256) [Link]

>> returning flags from functions
> You mean like the errno variable?

Not really, errno is a global variable - I was thinking of something very fast at the asssembly level, for the million of use case.
Something like (doesn't really work in C):

unsigned mybitfield, bitindex;
ifzeroset(bitno = ffs(mybitfield)) // ffs = find first bit set
error("none bit are set");
else
bitindex = bitno;

Translated in ia32 assembler by:
ffs eax,edx
jz call_error
mov edx,bitindex

You can replace ffs by strchr or any basic function which may not be inlined.
Maybe it could also be implemented in GCC by:
register struct flags {
unsigned zero : 1;
unsigned carry : 1;
...
} flags asm ("cc");
but the modification of flags by so many assembly instruction is probably a problem (reordering instructions optimisations).

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds