There was here an interesting discussion here Moving a record to private - #3 by Shuihuzhuan about setters and getters, and choosing between hiding or exposing data at record type level.
I have sometimes the in between situation.
If I want to create a type Person, exposing all fields except the age, I have to either make a bunch of setters and getters, or make two types, one being private (with other annoying constraints if they are in the same package due to the “premature” use of the type).
Let’s give an example in a language that have made the (wrong ) choice “Everything is a class” :
class Person {
public name: string; // The name is publicly accessible
private age: number; // The age is accessible only within the Person class
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public isAdult(): boolean {
return this.age >= 18;
}
}
What would be nice in Ada to have the ability to complete a type in the private part, something like:
package Person is
type Person is record with private
Name : Unbounded_String;
end record;
-- "record with private" is clear enough
-- or
type Person is record
Name : Unbounded_String;
with private;
-- More natural syntax to me, but possible confusion with predicates,
-- and taboo "end record" dropping :-)
function Is_Adult (P : Person) return boolean is (P.Age >= 18);
private
type Person private record is
Age : Natural;
end record;
-- It would be strange to have here a normal record type declaration
-- that wouldn't tell to the reader that other fields are already declared.
-- This is why there is an explicit "private record is",
-- close enough to what it actually means ("private additional part of the record is").
-- But you could object that it is already the case (not having all the fields at hand)
-- with tagged type, and that it not worth creating a new syntax for that reason.
end Persons;
From the semantic point of vue, it would be a normal private type, with the same constraints applying on the partial vue, except that public fields are in direct access, like if Setters and Getters where provided.
And for the sake of unification, this could also apply to tagged type private extension.
Does this proposal make sense?