Safe mode in D?

ProgrammingGhost dsioafiseghvfawklncfskzdcf at sdifjsdiovgfdisjcisj.com
Fri Oct 18 16:03:41 PDT 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?


More information about the Digitalmars-d mailing list