Hacker News new | past | comments | ask | show | jobs | submit login

Pascal doesn't really have enums as a language feature, so there is no direct analog. It has enums as an implementation detail, but for all intents and purposes the language could be implemented with something other than enums. The same is not true of C or Go, which make enums a first-class citizen of the language.

That is, with the caveat that Pascal does allow the union to be converted to its underlying enum representation. If we provide such conversion to get at the enum, then sure:

    Program Lander(output);
    type
      planet = (unknown, mercury);
    var
      landingOn: Integer;
    begin
      landingOn := Ord(mercury);
      writeln('Landing on ', landingOn);
      landingOn := 42;
      writeln('Landing on ', landingOn);
    end.
But you're ultimately comparing apples and oranges.



That program isn't correct, you are faking it by using an Integer.


Indeed. Just as I explained, Pascal doesn't really have language support for enums. The pseudo 'sum types' abstraction works to try and hide the fact that enums exist, only presenting the implementation detail enum if you explicitly convert a union element to its enumerated ordinal – which, as it happens, is an integer type.

The program is correct – it complies and runs just fine – but the original ask was faulty. Without any kind of real enum support in Pascal the language, there is no way to perform the same operations as is possible in languages that do have natural enum support. This program is the best you are going to do in the absence of that support. This was all detailed earlier.

Again, you are trying to compare apples and oranges. Rust[1] may try as it might to call sum types enums, but enums and sum types – along with Pascal's pseudo 'sum types' – are different models. I understand where your confusion stems from as there are a lot of similarities, but there is also a difference.

[1] And Swift originally, but its documentation is updated to make clear that, contrary to what the name implies, the language's enum construct is actually sum types and not enums.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: