D 2015/2016 Vision?

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 5 12:43:52 PDT 2015


On Monday, 5 October 2015 at 19:07:20 UTC, Meta wrote:
> On Monday, 5 October 2015 at 17:19:09 UTC, Gary Willoughby 
> wrote:
>> This can be shortened to:
>>
>> import std.stdio;
>> import std.typecons;
>>
>> class A
>> {
>> 	string name;
>>
>> 	this(string name)
>> 	{
>> 		this.name = name;
>> 	}
>>
>> 	void hello()
>> 	{
>> 		writeln("Hello, ", this.name);
>> 	}
>> }
>>
>> void main()
>> {
>> 	auto a = scoped!A("Foo");
>> 	a.hello();
>> }
>
> There's a critical flaw in `scoped`. Observe:
>
> import std.stdio;
> import std.typecons;
>
> class A
> {
> 	string name;
>
> 	this(string name)
> 	{
> 		this.name = name;
> 		writeln("Creating A");
> 	}
>
> 	~this()
> 	{
> 		writeln("Destroying A");
> 	}
>
> 	void hello()
> 	{
> 		writeln("Hello, ", this.name);
> 	}
> }
>
> void main()
> {
> 	auto a1 = scoped!A("Foo");
> 	a1.hello();
>
> 	A a2 = scoped!A("Foo");
> 	a2.hello();
> }
>
>
> The output:
>
> Creating A
> Hello, Foo
> Creating A
> Destroying A
> Destroying A
> object.Error: Access Violation

also:

// Error: can only synchronize on class objects, not 'Scoped'
auto a1 = scoped!A("Foo");
synchronized(a1) {}

and also:

// Error: template main.foo cannot deduce function from argument 
types !()(Scoped)
void foo(T)() if(is(T == A)) { }
void main(string[] args) {
     auto a1 = scoped!A("Foo");
     foo(a1);
}

     Bit



More information about the Digitalmars-d mailing list