October 18, 2013
On Friday, 18 October 2013 at 18:59:46 UTC, Andrei Alexandrescu wrote:
>
> I think one good compromise is to stick with the exact amount of flow control we currently have in constructors (which is primitive but quite adequate), and use that creatively. It's already implemented and works, so the implementation costs of applying it to other cases should be low.
>
> Andrei

Do you mean preventing calling constructor (from another constructor) within one control branch? May be this will be case #13.

import std.stdio;

extern(C) void _D4main1A6__ctorMFiZC4main1A(A a, int i);

class A
{
   this(int i) { writeln("reached"); }
   this()
   {
      int i = 1;
      //if (i)
      //   this(i); // Error: one path skips constructor
      if (i)
         _D4main1A6__ctorMFiZC4main1A(this, i);
      if (i)
         auto dg = __traits(getOverloads, this, "__ctor")[0](i);
   }
}

void main()
{
   new A;
}
October 18, 2013
On Friday, 18 October 2013 at 19:28:35 UTC, Maxim Fomin wrote:
> On Friday, 18 October 2013 at 18:59:46 UTC, Andrei Alexandrescu wrote:
>>
>> I think one good compromise is to stick with the exact amount of flow control we currently have in constructors (which is primitive but quite adequate), and use that creatively. It's already implemented and works, so the implementation costs of applying it to other cases should be low.
>>
>> Andrei
>
> Do you mean preventing calling constructor (from another constructor) within one control branch? May be this will be case #13.
>
> import std.stdio;
>
> extern(C) void _D4main1A6__ctorMFiZC4main1A(A a, int i);
>
> class A
> {
>    this(int i) { writeln("reached"); }
>    this()
>    {
>       int i = 1;
>       //if (i)
>       //   this(i); // Error: one path skips constructor
>       if (i)
>          _D4main1A6__ctorMFiZC4main1A(this, i);
>       if (i)
>          auto dg = __traits(getOverloads, this, "__ctor")[0](i);
>    }
> }
>
> void main()
> {
>    new A;
> }

Wanna talk about what can be done with reflection in "safe" C#?
October 18, 2013
On Friday, 18 October 2013 at 19:46:44 UTC, Max Samukha wrote:
> On Friday, 18 October 2013 at 19:28:35 UTC, Maxim Fomin wrote:
>> On Friday, 18 October 2013 at 18:59:46 UTC, Andrei Alexandrescu wrote:
>>>
>>> I think one good compromise is to stick with the exact amount of flow control we currently have in constructors (which is primitive but quite adequate), and use that creatively. It's already implemented and works, so the implementation costs of applying it to other cases should be low.
>>>
>>> Andrei
>>
>> Do you mean preventing calling constructor (from another constructor) within one control branch? May be this will be case #13.
>>
>> import std.stdio;
>>
>> extern(C) void _D4main1A6__ctorMFiZC4main1A(A a, int i);
>>
>> class A
>> {
>>   this(int i) { writeln("reached"); }
>>   this()
>>   {
>>      int i = 1;
>>      //if (i)
>>      //   this(i); // Error: one path skips constructor
>>      if (i)
>>         _D4main1A6__ctorMFiZC4main1A(this, i);
>>      if (i)
>>         auto dg = __traits(getOverloads, this, "__ctor")[0](i);
>>   }
>> }
>>
>> void main()
>> {
>>   new A;
>> }
>
> Wanna talk about what can be done with reflection in "safe" C#?

You are free to present horrible bugs of "unsafe" C#, but I bet
they are far, very far to DMD bugs/D design issues of "safe" D.
(By the way, I don't see why the code above provoked you to C#
talks).
October 18, 2013
On 10/18/2013 07:30 PM, H. S. Teoh wrote:
> @Walter: Just out of curiosity, what exactly is holding up the
> implementation of scope?
> ...

The missing design of scope, I guess.
October 18, 2013
On Friday, 18 October 2013 at 20:03:22 UTC, Maxim Fomin wrote:

> (By the way, I don't see why the code above provoked you to C#
> talks).

Because you:

1) Mentioned C# as a safer alternative to D.
2) Are using reflection to demonstrate D's unsafety.

Try this:

using System;
using System.Reflection;

namespace test
{
	class A
	{
		public int x;
		public A()
		{		
			x += 1;
		}
	}
	
	class App
	{
		public static void Main (string[] args)
		{
			var a = new A();			
			var ctor = a.GetType().GetConstructor(new Type[] {});
			ctor.Invoke(a, new object[] {});
			ctor.Invoke(a, new object[] {});
			Console.Write(a.x);
		}
	}
}











October 18, 2013
On Friday, 18 October 2013 at 22:29:45 UTC, Max Samukha wrote:
> On Friday, 18 October 2013 at 20:03:22 UTC, Maxim Fomin wrote:
>
>> (By the way, I don't see why the code above provoked you to C#
>> talks).
>
> Because you:
>
> 1) Mentioned C# as a safer alternative to D.
> 2) Are using reflection to demonstrate D's unsafety.
>
> Try this:
>
> using System;
> using System.Reflection;
>
> namespace test
> {
> 	class A
> 	{
> 		public int x;
> 		public A()
> 		{		
> 			x += 1;
> 		}
> 	}
> 	
> 	class App
> 	{
> 		public static void Main (string[] args)
> 		{
> 			var a = new A();			
> 			var ctor = a.GetType().GetConstructor(new Type[] {});
> 			ctor.Invoke(a, new object[] {});
> 			ctor.Invoke(a, new object[] {});
> 			Console.Write(a.x);
> 		}
> 	}
> }

I'm not sure why this is entirely bad. It looks like you're asking it to call the constructor a few times. If it allocates memory the GC should clean it up. Whats 'wrong' with this code?
October 19, 2013
Am 19.10.2013 00:29, schrieb Max Samukha:
> On Friday, 18 October 2013 at 20:03:22 UTC, Maxim Fomin wrote:
>
>> (By the way, I don't see why the code above provoked you to C#
>> talks).
>
> Because you:
>
> 1) Mentioned C# as a safer alternative to D.
> 2) Are using reflection to demonstrate D's unsafety.
>
> Try this:
>
> using System;
> using System.Reflection;
>
> namespace test
> {
>      class A
>      {
>          public int x;
>          public A()
>          {
>              x += 1;
>          }
>      }
>
>      class App
>      {
>          public static void Main (string[] args)
>          {
>              var a = new A();
>              var ctor = a.GetType().GetConstructor(new Type[] {});
>              ctor.Invoke(a, new object[] {});
>              ctor.Invoke(a, new object[] {});
>              Console.Write(a.x);
>          }
>      }
> }
>


There is nothing unsafe about this code snippet.

--
Paulo

October 19, 2013
On Friday, 18 October 2013 at 23:03:42 UTC, ProgrammingGhost wrote:

> Whats 'wrong' with this code?

Nothing, that's the point. Just like with __traits(getOverloads, this, "__ctor")[0](i).

In my world, safety features of a language are meant to help a fairly reasonable programmer avoid accidental mistakes. Misusing reflection or C interface is not an accidental mistake.
October 19, 2013
On Saturday, 19 October 2013 at 06:24:00 UTC, Paulo Pinto wrote:

>
> There is nothing unsafe about this code snippet.
>

Sure. There is nothing unsafe in misusing __traits and extern(C) as well.
October 19, 2013
Am 19.10.2013 08:58, schrieb Max Samukha:
> On Saturday, 19 October 2013 at 06:24:00 UTC, Paulo Pinto wrote:
>
>>
>> There is nothing unsafe about this code snippet.
>>
>
> Sure. There is nothing unsafe in misusing __traits and extern(C) as well.

There is if the result is

- memory corruption
- memory leaks no longer visible to the GC
- program crash
- ...