Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Richard Birkett

macrumors member
Original poster
Aug 21, 2011
30
0
I have a macro like this:

Code:
#define IntMethods(lower, cap, parameter) \
- (MMExpressionType)symboliccapWithInt:(SInt64)parameter\
{\
	return [(MMExpressionType)self symboliccap:[[MMTerm alloc] initWithInteger:parameter]];\
}\
\
- (MMExpressionType)lowerWithInt:(SInt64)parameter\
{\
	return [(MMExpressionType)self lower:[[MMTerm alloc] initWithInteger:parameter]];\
}

It is supposed to replace a repetitive set of implementations, I hope you understand what I am trying to do. My code here gets errors, it seems to think I'm literally using symboliccap etc. instead of something using the macro parameter.
Here is some desired output to input that will hopefully help if you don't understand.

Code:
IntMethods(add, Add, addend)
should be equivalent to
Code:
- (MMExpressionType)symbolicAddWithInt:(SInt64)exponent
{
	return [(MMExpressionType)self symbolicAdd:[[MMTerm alloc] initWithInteger:addend]];
}

- (MMExpressionType)addWithInt:(SInt64)exponent
{
	return [(MMExpressionType)self add:[[MMTerm alloc] initWithInteger:addend]];
}
Help greatly appreciated. :)
 
Thank you so much, that's perfect!
Here's my solution for reference:
Code:
#define IntMethods(lower, cap, parameter) \
- (MMExpressionType)symbolic##cap##WithInt:(SInt64)parameter\
{\
	return [(MMExpressionType)self symbolic##cap:[[MMTerm alloc] initWithInteger:parameter]];\
}\
\
- (MMExpressionType)lower##WithInt:(SInt64)parameter\
{\
	return [(MMExpressionType)self lower:[[MMTerm alloc] initWithInteger:parameter]];\
}
 
And when you get your macros working, you might consider whether you are not just trying to be too clever for your own good. Nothing more pleasant than looking for the definition of a method and not being able to find it anywhere in your source code. Or not being able to set a breakpoint in some method because of these macros.
 
And when you get your macros working, you might consider whether you are not just trying to be too clever for your own good. Nothing more pleasant than looking for the definition of a method and not being able to find it anywhere in your source code. Or not being able to set a breakpoint in some method because of these macros.

Well as you can see it is simple code really and they are all in the header anyway.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.