struct vs. class, int vs. char.

Denis Koroskin 2korden at gmail.com
Tue Apr 28 12:04:16 PDT 2009


On Tue, 28 Apr 2009 22:50:28 +0400, grauzone <none at example.net> wrote:

>> I belive it could be implemented as a library type, using Scope(T)  
>> template:
>
> I don't like that approach to replace random language features by  
> templates at all. And in this case, you really have to ask WHY?
>
> It's nice that it's possible, but there are some reasons that speak  
> against this approach:
> 1. it's harder to understand in general (even if the language definition  
> becomes smaller)
> 2. error messages turn into an incomprehensible mess
> 3. puts more stress on the compiler and it gets slower (you will  
> understand when you have to _wait_ for your project to finish  
> compilation, even if you use the superfast dmd)
>
>> class Foo
>> {
>>     Scope!(Bar) _bar;
>> }
>
> Would typeof(_bar) == Bar?
>

No way! But Scope!(Bar) would be implicitly castable to Bar (just not vice-versa):

Bar b = _bar; // allowed
_bar = b; // disallowed

In fact, I don't like scope as it is:

scope Foo foo = ...; // is foo scope allocated or not? It depends:

scope Foo foo = new Foo(); // scope-allocated

Foo getFoo() { return new Foo(); }
scope Foo foo = getFoo();  // ????

In last example, DMD may inline and make it scope-allocated, but may not inline and make it heap-allocated.

Scope!(Foo) foo = ...;

gives you better control over allocation. For example,

Scope!(Foo) foo = getFoo();

won't even compile.



More information about the Digitalmars-d mailing list