A comparison between C++ and D

maik klein via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 8 18:24:19 PST 2016


On Wednesday, 9 March 2016 at 02:14:34 UTC, Chris Wright wrote:
> On Wed, 09 Mar 2016 01:18:26 +0000, maik klein wrote:
>> [...]
>
>> [...]
>
> Rather, declaring a variable should never throw an exception. 
> Declaring a variable shouldn't create any nontrivial work.
>
> It's trivial to create a type in D that doesn't have exception 
> free default construction:
>
> class A {
>   this() { throw new Exception(); }
> }
That is why I have limited it to `structs`, but I see that I have 
used `every type` before which is just wrong. Not sure why I even 
wrote it.

>> [...]
>
> D has one explicit cast operator. It can do much of what 
> static_cast does, though D does not let you downcast without a 
> runtime check.
I should probably be more clear that this is about compile time.
>> [...]
>
> You don't need the GC; you just need storage that's not on a 
> stack frame below where you're catching the exception.
>
> You can malloc an exception and throw it.
>
> You can allocate space for an exception in the static data 
> region and throw it from there, like with OutOfMemoryError.
>
> You can allocate space for an exception on the stack in main() 
> and pass it down the call chain.
Thanks, I did not know this. Will fix it asap.
>> [...]
>
> That's the canonical way of doing it, but with dub, I'm seeing 
> people generally adding the project name before that. So, for 
> instance, I have module "roguelike.levelgen" in source file 
> "source/levelgen.d". It works.
>
>> [...]
>
> stringof evaluates an expression or type and produces a string 
> representation for that value. That string representation is a 
> compile- time constant. It doesn't print it at compile time or 
> runtime.
>
> Does writeln print values at compile time? That would be kind 
> of strange. Maybe useful in the context of CTFE, but still a 
> little unexpected.

I should be more clear. I meant writeln for runtime printing and 
pragma(msg) for compile time printing.



More information about the Digitalmars-d mailing list