How can one declare two types- for example doubles- so that they are never under any circumstances directly compatible?
type
TMetres: double;
TKilometres: double;
const
altitudeLimit: TMetres= 1500.0;
rangeLime: TKilometres= 20.0;
They should not be assignment compatible, they should not be compatible as parameters (i.e. one is promoted to the other if a function taking it as parameter is undefined), and neither should be compatible with the base type (here, double, but the same might apply to integers).
MarkMLl
I asked that question long ago. I've been wanting to do that for ages. There is no way to do it in a _convenient_ manner.
If you don't mind some inconvenience then it can be done like this:
type
ATrulyNewType = record
v : double;
end;
Unfortunately that means the value can only be accessed using <variable_name.v> which gets old quickly. Since I tried this years ago, I remember that by typecasting ATrulyNewType to double you can sometimes dispense of having to qualify the variable name with the field.
What I also remember is that it was still quite inconvenient. I abandoned every idea because none were simple and straightforward enough.
Supposedly but, it doesn't work, something like this is supposed(?) to work:
type
TNew = type integer;
but, TNew is still assignment compatible with integer which means it is not really a new/different type.
Hopefully someone can provide a solution because I'd love to have it too.
I think it would be a really great feature if FPC gave some way of making the type definitions you showed _truly_ new and different types. However, that means that assigning a value to it would require typecasting the value which is a small price to pay for a truly new type.