Hacker News new | ask | show | jobs
by Galanwe 1021 days ago
It's difficult for someone with 0 knowledge of the language to really understand representation clauses. It does seem to be similar to enum values in C++?

    // Ada
    for Day use (Mon => 2#00000001#,
                 Tue => 2#00000010#,
                 Wed => 2#00000100#,
                 Thu => 2#00001000#,
                 Fri => 2#00010000#,
                 Sat => 2#00100000#,
                 Sun => 2#01000000#);
    // C++
    enum Day {
        Mon = 0b00000001,
        Tue = 0b00000010,
        Wed = 0b00000100,
        Thu = 0b00001000,
        Fri = 0b00010000,
        Sat = 0b00100000,
        Sun = 0b01000000,
    };
As for the record representation, my understanding is that it is equivalent to having a normalized __attribute__((__packed__)), where smart compiler padding is disabled and you can arbitrarily decide the memory layout of your struct?
1 comments

Representation clause is just language defined way of how struct (Record in Ada) Enum or Array are actually laid out in memory. So, for example, you can define a struct that represents a register, overlay that register address and use it like this:

  procedure Enable_USB_Clock is
  begin
     Registers.PMC_Periph.PMC_SCER.USBCLK := 1;
  end Enable_USB_Clock;
Example taken from here: https://learn.adacore.com/courses/Ada_For_The_Embedded_C_Dev...