Thread overview
How should this be done? (1.0)
Feb 05, 2008
Michael Coupland
Feb 05, 2008
Derek Parnell
Feb 06, 2008
Michael Coupland
Feb 06, 2008
Derek Parnell
Feb 06, 2008
Christian Kamm
February 05, 2008
The following doesn't work because I'm trying to use a template mixin like a string constant mixin, and you can't actually do calculations in a template like this. (You get "no identifier for declarator" errors on the assignment lines.) What is the Proper D Way to do this? (I'd rather not use a function that returns a struct; in C/C++ one would probably use a macro.) I considered a heredoc string and doing a proper textual mixin, but that feels like overkill...

	Michael

-----------------------------------------------

template CommonCalculations()
{
    bool commonBool1;
    bool commonBool2;
	
    {
        int LocalVal = SomeFn();
        commonBool1 = SomeOtherFunc();
	commonBool2 = AThirdFunction(LocalVal,commonBool1);
    }
};

// (...)

{
	mixin CommonCalculations!();
	// do stuff here with commonBool1 and commonBool2
}

February 05, 2008
On Mon, 04 Feb 2008 21:49:50 -0800, Michael Coupland wrote:

> I considered a heredoc string and doing a proper textual mixin, but that feels like overkill...

What is 'overkill' about this ...

const char[] CommonCalculations =
r"
      bool commonBool1;
      bool commonBool2;

      {
          int LocalVal = SomeFn();
          commonBool1 = SomeOtherFunc();
          commonBool2 = AThirdFunction(LocalVal,commonBool1);
      }
"
. . .

{
 	mixin (CommonCalculations);
	// do stuff here with commonBool1 and commonBool2
}


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
5/02/2008 6:10:32 PM
February 06, 2008
Correct me if I'm wrong, but I think it has several of the same problems that a normal macro has. Compiler errors in the heredoc string will be reported on the wrong line (presumably debuggers would have similar problems) and if you want to parameterize your mixin you run the risk of textual duplication. There's also the annoyance that text editors may fail to properly format the text (having an editor that doesn't recognize my heredoc syntax is helpful in this case.)

Derek Parnell wrote:
> On Mon, 04 Feb 2008 21:49:50 -0800, Michael Coupland wrote:
> 
>> I considered a heredoc string and doing a proper textual mixin, but that feels like overkill...
> 
> What is 'overkill' about this ...
> 
> const char[] CommonCalculations =
> r"
>       bool commonBool1;
>       bool commonBool2;
>  	
>       {
>           int LocalVal = SomeFn();
>           commonBool1 = SomeOtherFunc();
>           commonBool2 = AThirdFunction(LocalVal,commonBool1);
>       }
> "
> . . .
> 
> {
>  	mixin (CommonCalculations);
> 	// do stuff here with commonBool1 and commonBool2
> }
> 
> 
February 06, 2008
What about the old fashioned way:

struct CommonCalculation
{
    bool commonBool1;
    bool commonBool2;

    void calculate()
    {
        int LocalVal = SomeFn();
        commonBool1 = SomeOtherFunc();
        commonBool2 = AThirdFunction(LocalVal,commonBool1);
    }
}


{
    CommonCalculation calc;
    calc.calculate();
    // do stuff here with calc.commonBool1 and calc.commonBool2
}

You could also make the calculate method a static opCall(), which would reduce the invocation to the line

CommonCalculation calc = CommonCalculation();

But imho that's not much shorter and less obvious.

Christian Kamm
February 06, 2008
On Tue, 05 Feb 2008 21:54:28 -0800, Michael Coupland wrote:

> Correct me if I'm wrong, but I think it has several of the same problems that a normal macro has.

I see what you're saying. It could be that the future AST macros can help us with this sort of stuff.


However, it appears that you'd like to define a named fragment of code that includes arbitrary declarations and executable statements, force that fragment to be in-lined in one or more places (with possibly parameterized textual replacement?), and have any error messages refer to line numbers in the fragment's definition rather than, or as well as, its instantiation location.

Example syntax (not wedded to this of course).

macro CommonCalculation(alias b1 = defBool1, alias b2 = defBool2)
{
    bool b1;
    bool b2;

    void calculate()
    {
        int LocalVal = SomeFn();
        b1 = SomeOtherFunc();
        b2 = AThirdFunction(LocalVal,b1);
    }
}

...

{
    CommonCalculation(commonBool1, commonBool2);
    // do stuff here with commonBool1 and commonBool2
}

...

{
    CommonCalculation();
    // do stuff here with defBool1 and defBool2
}

With messages like ...

test.d(231): Function 'someFn' not defined. Referenced in macro
'CommonCalculation'(line 48).
test.d(493): Function 'someFn' not defined. Referenced in macro
'CommonCalculation'(line 48).


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
6/02/2008 6:01:14 PM