[M3devel] ternary operator

Hendrik Boom hendrik at topoi.pooq.com
Wed Dec 29 16:38:16 CET 2010


On Wed, Dec 29, 2010 at 12:45:58PM +0000, Jay K wrote:
> 
> C code:
> 
>     m.swap32 = swapped ? swap32 : no_swap32;
>     m.swap64 = swapped ? swap64 : no_swap64;
>     m.macho_header_size = m64 ? sizeof(macho_header64_t) : sizeof(macho_header32_t);

Algol 60 code:

    m.swap32 := IF swapped THEN swap32 ELSE no_swap32;
    m.swap64 := IF swapped THEN swap64 ELSE no_swap64;
    ...(but no such thing as sizeof)...

Algol 68 code:

    m.swap32 := IF swapped THEN swap32 ELSE no_swap32 FI;
    m.swap64 := IF swapped THEN swap64 ELSE no_swap64 FI;
    ...(but no such thing as sizeof)...

> 
> 
> Modula-3?
> 
> 
>   file.swap32 := no_swap32;
>   file.swap64 := no_swap64;
>   IF swapped THEN
>     file.swap32 := swap32;
>     file.swap64 := swap64;
>   END;
>   file.header_size := (ORD(m64) * BYTESIZE(header64_t)) + (ORD(NOT m64) * BYTESIZE(header32_t));
> 
> 
> 
> I don't like either of those forms nor
> 
> 
>   IF swapped THEN
> 
>     file.swap32 := swap32;
> 
>     file.swap64 := swap64;
> 
>   ELSE
> 
>     file.swap32 := no_swap32;
> 
> 
>     file.swap64 := no_swap64;
> 
> 
>   END;
> 
> 
> 
> If static type inference was more prevalent, I wouldn't mind like:
> 
> 
>     m.swap32 = {no_swap32, swap32}[ORD(swapped)];
> 
>     m.swap64 = {no_swap64, swap64}[ORD(swapped)];
> 
>     m.macho_header_size = {sizeof(macho_header32_t), sizeof(macho_header64_t)}[ORD m64];
> 
> 
> 
> but I assume {no_swap32, swap32} has to be a typed constant...

Abd if it no_awap32 and swap32 were como,licated expressions, it would 
waste time evaluating things never used.

> 
> 
>  - Jay
>  		 	   		  



More information about the M3devel mailing list