[fpc-pascal] sentinels, "marks", enumerations

spir ☣ denis.spir at gmail.com
Wed May 19 14:00:49 CEST 2010

[follow-up from the thread about "void"]

To favor the use of custom sentinels, I'm thinking at a Mark type. This is not strictly necessary, since the user can build individual marks or series of them easily. Either as distinct referenced objects or as plain values (logical, number, string...).
This type would be make explicite the user's intention by using a dedicated type. Marks are basically value-less things. So they could simply be made distinct things. Like when creating a sentinel with "NOT_FOUND = Object()". (In my case it would then use Mark instead of object.)

But giving them a distinct value can also be useful, so I could use nominals (http://en.wikipedia.org/wiki/Nominal_number), *arbitrary* numbers used to identify value-less things. A mark would then have a name for output and a number as ID. The type Mark would automatically increment numbers. But the user can also set the number if useful.

This idea leads me to a feature similar to Pascal-like "enumerations" (weirdly called, imo): a set or sequence of marks, like for card suites. This would be done by the user subtyping Mark: then the nominals restart at 0 or 1. I just need a nice syntax to define all marks of a given type together, in both cases where the user sets the numbers or not:
    Suite : Mark.subType ["CLUB" "DIAMOND" "HEART" "SPADE"]
    Suite : Mark.subType ("CLUB":1 "DIAMOND":2 "HEART":3 "SPADE":4)
Actually, new marks would be made slots of their type, under their own name, so that one would then write eg "Suite.SPADE". I prefere this than having SPADE in the global scope.

Since the nominals are ordered, marks of the same type inherit this quality and can be compared for '<' (like in the game of bridge where card suites have different strength).
Also, since the user can explicitely set the number of a given mark (which then is no more a nominal), this can be used in the app. Eg a user can make a CardValue subtype of Mark, to identify cards in a suite. And later use the card values to count points.

The here missing feature Pascal offers is that a Mark subtype is not an ordinal type. It makes an ordered set in the sense marks can be compared for inequality; but it cannot be used eg to define an interval like
    highFigures : CardValue.J..CardValue.A
(I don't know the card names in english, only their initial ;-)

Equivalent marks are then simply set equal nominals. But I wonder if I shouldn't make marks referenced values (things) instead... (meaning test of equality doesn't even check the value number). Then, equivalent marks must be aliases of the same thing.


vit esse estrany ☣


More information about the fpc-pascal mailing list