PDA

View Full Version : {$define}



Bijo
08-07-2013, 10:08 PM
If my memory serves me well and if I am properly reminded of the preprocessor that I used with C/C++ long ago, then what I'm about to ask is probably not what's the case with {$define}. I will put it forth anyway :P

In the FPC Compiler Reference it says that this can be used to define a symbol. Also, in the FPC Programmer's Guide it says that "The directive {$DEFINE name} defines the symbol name. This symbol remains defined until the end of the current module (i.e. unit or program), or until a $UNDEF name directive is encountered."

Of course, I would think that therefore I could do something like this: it's possible to assign my own symbols for use in the language. For example, it is possible to choose ~ as a symbol for not, and that the compiler will actually know and treat ~ as not. This seems to be entailed by the description that I read.

For instance, maybe I could try something like


{$define ~ not}

or


{$define ~ = not}

or some such thing?

Of course, I tested it and it doesn't work. Is it at all even possible to do such a thing?

If {$define} is used for defining some sort of difficult formula, then how exactly should this thing be used? Can you give an example?

Super Vegeta
08-07-2013, 10:41 PM
{$DEFINE}, by default, can only be used for conditional compilation, for example:

{$DEFINE DEVELOPER} //Comment this out when making a release!
(* strip some lines *)
{$IFDEF DEVELOPER}
(* add some cheat codes here for easier debugging *)
{$ENDIF}

What you are speaking of are macros. FPC has very limited support for these, eg. C/CPP preprocessor allows for macros with arguments (like min(a,b)), whereas FPC macros can only be used for simple word substitution. Macros are disabled by default, so you need to enable them using the {$MACRO} directive. After that, you can declare them using {$DEFINE #Macro# := #Replace_by#} notation, eg.

{$MACRO ON} {$DEFINE AnsiArr := Array of AnsiString}
Var StrArr : AnsiArr;

Bijo
09-07-2013, 06:29 PM
Ah, so no way to actually replace not with ~ :(
~ looks much clearer and cleaner to my eyes than not.



if not(x = 0) then
...



if ~(x = 0) then
...

Well, not certainly looks cleaner and clearer than ! :p

phibermon
09-07-2013, 08:01 PM
No you can't do things like that... well, perhaps you can but it's not a good idea, do you want others to understand your code? make them hunt for the defines just to check on types etc? plus I bet it causes a nightmare when debugging etc. "was that an array of words or longs? *check*" compared to "that's definitely an array of words, because that's what's written". You really shouldn't care about how the code looks to your eyes, it shouldn't even be the least of your concerns.

It's the structure that's beautiful, the designs that are elegant - it transcends mere syntax :)

And replacing 'not' with '~' goes against everything that Pascal was designed for. It uses English words for a reason, your brain will process them faster than symbols.

You can scan really quickly over Pascal code, you have to keep a very careful eye out when trying to scan even slowly over C++ code (unless you do it every day for a living and even then case sensitivity is really not helpful! our brains don't take note of upper/lower variations in the same way it does the word itself, mistakes *will* be made that aren't even possible in pascal)