Adding a new design constraint to D
forkit
forkit at gmail.com
Tue Jun 14 07:34:43 UTC 2022
On Tuesday, 14 June 2022 at 07:12:18 UTC, Salih Dincer wrote:
>
> If the object in the same module is visible, why make it
> private when you're already writing code? You are already
> partially isolating using underscore (Foo._id). For example,
> Foo.length will never conflict.
>
> What difference would it make to see or not see this error?
>
>> Error: no property `id` for type `source.Foo`
yes, 'private' in this case is pointless.
but the example, is the kind of code I, and many like me, will
come to D and write, expecting that private means private to the
scope of the class.
Since private means private to the scope of the module, how then
do I (and those like me) specifiy that intent in D? That's the
question that has prompted this idea of adding a scope private
option.
Also, while some use the underscore convention to indicate
something is scope private, it doesn't necessarily follow that
anyone who used underscore was specifying that to be scope
private.
So isolating using an underscore doesn't really indicate intent,
unless that was the intent, and it also depends on other
considerations (e.g you teams coding style, etc).
I'm talking about an option that explicitely, unmistakingly,
shows intent, and furthermore, an intent the compiler recognises
and enforces at compile time.
private(scope) <- the intent cannot my inferred here, it is
explicit.
_var <- does that underscore infer an intent for scope private??
You need additional information to make that judgement.
An underscore 'convention' is no substitute to an explicit intent
that cannot be missinterpreted or mistakingly bypassed - the
compiler will know my intent, and enforce it, and anyone
reading/modifying/using my code will know my intent also.
the same conclusion must also apply to the one-class-per-module
argument.
it's about knowing the intent, vs guessing what the intent was
(or needing to obtain more information to understand the intent -
e.g is it because our coding style say use an underscore to
indicate this is scope private).
the compiler cannot infer why you used an underscore, nor can it
infer why you put a class in a file by itself.
More information about the Digitalmars-d
mailing list