PDA

View Full Version : C-style #DEFINEs



Vandalf
06-03-2017, 18:59
Consider this code:

Function HALQuote(myname as string) as string
return "I'm sorry, "& myname &", I'm afraid I can't do that."
end function
When "HALQuote(*)" is evaluated, thinBasic has to bear the runtime costs of one function call, checking the argument's type, allocating two strings, and copying the argument's value twice. Compare that to the following:

'#define HALQuote(myname) "I'm sorry, "& myname & ", I'm afraid I can't do that."
When this "HALQuote(*)" appears in source code, thinBasic only needs to allocate one string and copy the argument's value once, without calling a function and without checking types. Although there will be some increased cost in preprocessing time or even runtime, depending on how this concept gets implemented in thinBasic. Also, macros like that must be used carefully, but I think this would be a nice option to have.

ErosOlmi
06-03-2017, 22:11
I'm sorry, Vandalf, I'm afraid I can't do that.

:D sorry, Just joking a bit.

Considering thinBasic fully interpretative engine ... this would not speed thinBasic execution unless that function is executed some many thousands of times.
Macros is something I tried to implement in the past but never had enough time to dedicate to them.

thinBasic has a lot of other functions able to simplify work.
For example your example can be written in something like that:

string MyName = "Vandalf"msgbox 0, Expand$("I'm sorry, $myName, I'm afraid I can't do that.")
'---Or
msgbox 0, StrFormat$("I'm sorry, {1}, I'm afraid I can't do that.", MyName)


http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?expand$.htm
http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?strformat$.htm

Anyway I understand what you mean, I will add to my ToDo list.

Thanks for testing thinBasic.
Eros