struct vs. class, int vs. char.
Denis Koroskin
2korden at gmail.com
Tue Apr 28 11:34:22 PDT 2009
On Tue, 28 Apr 2009 21:42:51 +0400, grauzone <none at example.net> wrote:
> Jarrett Billingsley wrote:
>> On Tue, Apr 28, 2009 at 12:07 PM, MLT <none at anon.com> wrote:
>>
>>> What I don't like is that it seems that structs and classes should
>>> almost be interchangeable - one might implement something as a class
>>> and later want it to be a struct, or vice versa. It almost is actually
>>> a local decision. I might want something to be a class in one place,
>>> and a struct in another.
>> I hear this all the time from C++ users. But in practice, it
>> virtually never comes up. I have never, in my five years of using D,
>> wanted to change a class to a struct or vice versa, or ever seen
>> anyone else doing that (or complaining about its difficulty). The
>> only people who complain are those who don't use the language ;)
>
>
> Often I'd wish to statically allocate an object in the current context,
> like you can do with a struct. scope partially gives me what I want. It
> would be nice if "scope" could be used in other places:
>
> class Bla {
> int member;
> }
>
> class Foo {
> scope Bla b; //object constructed here
> }
>
> static assert(Foo.classinfo.init.size == 5*4);
>
> Yeah, you'd need a solution for how to call the ctor. Maybe the (very
> annoying) way of how initialization of final/const members is handled
> would be useful here.
I belive it could be implemented as a library type, using Scope(T) template:
class Foo
{
Scope!(Bar) _bar;
}
Perphaps, Phobos could provide that functionality, then we could deprecate "scope Foo foo = new Foo(args);" in favor of "auto foo = Scope!(Foo)(args);" and eventually remove from language.
More information about the Digitalmars-d
mailing list