Re: Enumeration literals and their associated functions



"Randy Brukardt" <randy@xxxxxxxxxxxxxx> writes:

> Right. The classic example is:
>
> type Base is (Bin, Oct, Dec, Hex);
> type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov,
> Dec);

I thought the classic example was Rainbow_Color and Traffic_Light,
both of which have a Red. ;-)

> Both enumerations have literals Oct and Dec. If these were treated as
> objects, that would be illegal. So the language treats them as intrinsic
> functions instead. Then
> This_Month : Month := Oct;
> works, because the standard overloading rules apply.

Yeah, that's probably the reason. The reasoning seems to be:

1. Only subprograms should be overloadable.
2. But it would sure be nice if enumeration literals were overloadable.
3. Therefore, we had better pretend that enum lits are functions.

It seems to me it would be better, after recognizing (2), to say:

3. Therefore, (1) must be wrong. It should be replaced by "Only
subprograms and enumeration literals should be overloadable."

Pretending enum lits are functions is just plain confusing,
and provides no important functionality.

And, perhaps:

4. If enum lits are overloadable, then constants should be overloadable,
too.

After all, one might want to change type Base above (for various good
reasons) to:

package ... is
type Base is private;
Bin: constant Base;
Oct: constant Base;
...
private
...
end ...;

But now any place that uses both Base and Month might become ambiguous,
and have to be changed, for no good reason.

> Practically, you can just think of them as overloaded literals. It probably
> makes more sense that way. The definition as a function is primarily to make
> the language rules more consistent (and simpler); it's not some sort of
> important feature.

More consistent, perhaps. Certainly not simpler.

- Bob
.



Relevant Pages

  • Re: Hashtable
    ... It is occasionally pointed out that although Java claims operator ... overloading is bad (which is one of the reason Java doesn't allow operator ... overloading), it's somewhat hypocritical that Java overloads the + operator ... So coming up with a new operator for string concatenation might be seen ...
    (comp.lang.java.programmer)
  • Re: Python and Flaming Thunder
    ... catch statement is that the error path is separate from the normal ... It also looks like an overloading of catch, ... exception-handling mechanism. ... equal to the l-value at the end, that's fine, but there's no reason ...
    (comp.lang.python)
  • Re: Enumeration literals and their associated functions
    ... > I believe that the LRM defines enumeration literals as functions ... > because this automatically defines their overloading. ... because the standard overloading rules apply. ... the language rules more consistent; ...
    (comp.lang.ada)
  • Re: string == and Equals
    ... I'm sure there's a good reason, but it's escaping me at the moment. ... with operator overloading, although I'm not sure of the merit of them. ... I was under the mistaken impression that this was something the compiler could have implemented. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Object REXX is a s***...
    ... overloading, but I don't find gratuitous overloading. ... Operator overloading seems to invite abuse for some reason. ... I can see its utility eg. for matrix math, where the overloaded operators are intuitive and do what most people would expect them to do. ... In my experience, too many programmers do not have the discipline not to abuse certain language features, so the length of rope provided by the language ought to be limited;) ...
    (comp.os.os2.programmer.misc)