What would break if class was merged with struct
Moritz Maxeiner via Digitalmars-d
digitalmars-d at puremagic.com
Sat May 27 12:01:12 PDT 2017
On Saturday, 27 May 2017 at 18:42:02 UTC, Ola Fosheim Grøstad
wrote:
> On Saturday, 27 May 2017 at 18:26:07 UTC, Moritz Maxeiner wrote:
>> mechanism, but you *cannot* use a smart pointer as a
>> replacement for a class instance, because the latter is normal
>> pointer, and the former adds features on top of a pointer;
>> they cannot be the same as a matter of definition, i.e. you
>> cannot replace classes with smart pointer structs on the
>> language level, because it removes features from the language.
>
> I am not sure what you mean here. As long as the "smart
> pointer" is in a subtype relationship with the current "class
> reference" then it can add features, yes?
An example, then:
---
module a;
class Foo {}
module b;
import a;
void bar(Foo foo) {}
---
Here, `bar`, takes a (pointer to a) class instance as parameter
`foo`. `foo` is a single pointer, i.e. 8 bytes on a 64bit OS,
with *no* special semantics.
You cannot replace the meaning of type `Foo` here with something
that is not also exactly 8 bytes (on 64bit OS) with no special
semantics, because that changes the semantics of `bar`. i.e.
`Foo` must not be a smart pointer because a smart pointer
necessarily introduces features (and thus changes the semantics).
And this is exactly what you were proposing in the sentence I
replied to; you were proposing to essentially change the
semantics of every use of a class instance in all D code by
rewriting `Foo` into a smart pointer.
>
> The point of this thread was to have fewer language constructs,
And my reply was "if you want to do this, it must use a normal
pointer, not a smart pointer, because that changes semantics". If
you want a smart pointer, use one, but don't suddenly make
something that isn't a smart pointer by language design change
shape into a smart pointer.
> with more lowering, so that the resulting language would be a
> super-language, that fully covers the original language (or
> close to it).
Sure, but then type `Foo` in the above must remain a normal
pointer, not become a smart pointer.
More information about the Digitalmars-d
mailing list