[M3devel] ternary operator

Jay K jay.krell at cornell.edu
Wed Dec 29 13:45:58 CET 2010


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);


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...


 - Jay
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20101229/0079918f/attachment-0001.html>


More information about the M3devel mailing list