www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Conditionally set nothrow: for a block of code.

reply Mike Franklin <slavo5150 yahoo.com> writes:
I'm trying to find a way to declare a block of code `nothrow:` 
when compiling with -betterC, but not `nothrow` when not 
compiling with -betterC.

The solution is needed for this PR:  
https://github.com/dlang/druntime/pull/2184/files#r188627707


----------
void test() { }

version(D_BetterC)
{
     nothrow:
}

extern(C) void main()
{
     test();   // This should throw an error in -betterC because 
`main` is
               // `nothrow` but `test` isn't. It doesn't work
}


----------
version(D_BetterC)
{
     enum isNoThrow = true;
}
else
{
     enum isNoThrow = false;
}

void test() { }

static if (isNoThrow)
{
     nothrow:
}

extern(C) void main()
{
     test();   // This should throw an error in -betterC because 
`main` is
               // `nothrow` but `test` isn't. It doesn't work
}


----------
version(D_BetterC)
{
     enum nothrowValue = "nothrow:";
}
else
{
     enum nothrowValue = "";
}

void test() { }

mixin(nothrowValue);

extern(C) void main()
{
     test();   // This should throw an error in -betterC because 
`main` is
               // `nothrow` but `test` isn't. It doesn't work
}


Given that the PR above is for object.d, I can't turn the entire 
object.d source file into a string and conditionally mix that in. 
  Does anyone have a solution to this?

Thanks,
Mike
May 24 2018
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/24/18 2:51 PM, Mike Franklin wrote:

 Given that the PR above is for object.d, I can't turn the entire 
 object.d source file into a string and conditionally mix that in.  Does 
 anyone have a solution to this?
My recommendation would have been the last one, but if that doesn't work, I don't think anything will that is... palatable. The only other avenue you *could* explore is importing a file as a string and mixing the whole thing in (prepending "nothrow:" at the top). Other than that, I think it's on to DIP territory. -Steve
May 24 2018
prev sibling next sibling parent Uknown <sireeshkodali1 gmail.com> writes:
On Thursday, 24 May 2018 at 18:51:31 UTC, Mike Franklin wrote:
 I'm trying to find a way to declare a block of code `nothrow:` 
 when compiling with -betterC, but not `nothrow` when not 
 compiling with -betterC.

 The solution is needed for this PR:  
 https://github.com/dlang/druntime/pull/2184/files#r188627707
 [...]
 Given that the PR above is for object.d, I can't turn the 
 entire object.d source file into a string and conditionally mix 
 that in.
  Does anyone have a solution to this?

 Thanks,
 Mike
I think conditional application of attributes would be something useful. Something like this: version (D_BetterC) enum BetterC = true; else enum BetterC = false; nothrow!(BetterC): ... Of course that would require a DIP though
May 27 2018
prev sibling next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Thursday, 24 May 2018 at 18:51:31 UTC, Mike Franklin wrote:
 I'm trying to find a way to declare a block of code `nothrow:` 
 when compiling with -betterC, but not `nothrow` when not 
 compiling with -betterC.

 [...]
Not one now but, DIP 1012 will allow this as nothrow will become a regular enum attribute.
May 28 2018
prev sibling parent Mike Franklin <slavo5150 yahoo.com> writes:
On Thursday, 24 May 2018 at 18:51:31 UTC, Mike Franklin wrote:
 I'm trying to find a way to declare a block of code `nothrow:` 
 when compiling with -betterC, but not `nothrow` when not 
 compiling with -betterC.

 The solution is needed for this PR:  
 https://github.com/dlang/druntime/pull/2184/files#r188627707
Walter came up with a solution to this; put the code that is conditionally `nothrow` in a template and mix it in in the appropriate branch. void test() { } mixin template code() { extern(C) void main() { test(); // This should throw an error in -betterC because `main` is // `nothrow` but `test` isn't. It doesn't work } } version(D_BetterC) { nothrow: mixin code; } else { mixin code; } Thanks, Walter. Mike
May 28 2018