Page 2 of 3

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 8:23 am
by Blue
PB wrote:[...]Just mention in your docs that I came up with the idea, then. Thanks.
You're kidding i hope. :shock:
PB wrote: Here's a practical example of how it saves typing, in conjunction with regular macros too:

Code: Select all

Macro HKCU
  #HKEY_CURRENT_USER
EndMacro

StringMacro $SF
  Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
EndStringMacro

appdata$=ReadRegString(HKCU,"$SF","AppData")
history$=ReadRegString(HKCU,"$SF","History")
...
Your example only illustrates perfectly how and when a string constant should be used:

Code: Select all

Macro HKCU
  #HKEY_CURRENT_USER
EndMacro

#SF = "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

appdata$=ReadRegString(HKCU,#SF,"AppData")
history$=ReadRegString(HKCU,#SF,"History")
...
...
My conclusion: you're not even aware that text can be assigned to a constant in PureBasic.

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 8:52 am
by PB
> Your example only illustrates perfectly how and when a string constant should be used

In that particular example, yes. But that's not what the idea is limited too. For string building it's superior to constants and involves far less typing.

> My conclusion: you're not even aware that text can be assigned to a constant in PureBasic

You're kidding i hope. :shock:

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 2:36 pm
by Trond

Code: Select all

StringMacro PB
  PureBasic
EndStringMacro

MessageRequester("","This PB example was coded in PB")
MessageRequester("", "Leave this idea in the CUPBOARD!")
A good idea? Absolutely not. We need a standard mask for this to be of any use.

A better solution would be to automatically concatenate strings:

Code: Select all

#PB = "PureBasic"

MessageRequester("","This" #PB "example was coded in" #PB)
MessageRequester("", "This is one " "long string.")
This is a tried and tested solution (used in C) that works. IMHO it's the best feature of C. This way, existing code won't break.

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 4:42 pm
by Blue
Trond wrote:A better solution would be to automatically concatenate strings:[...]
This is a tried and tested solution (used in C) that works.
+10
Plus it would enhance the usability of the existing string constant feature.
Trond wrote:[...]This way, existing code won't break.
Exactly.    +20 !


In the meantime, the following comes pretty close, given the currently available syntax:

Code: Select all

Macro mPB
 +" PureBasic "+
EndMacro

Debug "This" mPB "debug output uses a" mPB "string macro.")
MessageRequester("","This" mPB "example was coded in" mPB "")     ;; <------ terminating empty string required here !
;; MessageRequester("", "This is one " "long string.")    ;; <------ syntax of my dreams!
But it lacks the flexibility of Trond's proposed solution.

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 8:00 pm
by Fred
Well just add the "+" and you're done:

Code: Select all

#PB = "PureBasic"

MessageRequester("","This "+#PB+" example was coded in "+#PB)
MessageRequester("", "This is one "+"long string.")
String are internally merged by the compiler.

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 8:03 pm
by Trond
I know that they are merged when you use +. The purpose of leaving out the + was only to aid readability.

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 9:17 pm
by netmaestro
I like the automatic string concatenation idea, that's a good feature request on its own.

Re: Macros for literal strings

Posted: Tue Oct 06, 2009 10:18 pm
by Kaeru Gaman
well, honestly I would say, leaving out the PLUSes would drastically decrease readability.

in BASIC there have always been PLUSes for String combination,
and their strong advantage is, when you glance at a single doublequote and a plus,
you can see at once wich side is inside the string and wich outside.
Imagine a combination of six partstrings with five variables without pluses - sheer horror.

Maybe people who grew up with other languages than Basic would be happier without pluses - people who grew up with Basic will not.

Re: Macros for literal strings

Posted: Wed Oct 07, 2009 1:17 am
by Little John
Kaeru Gaman wrote:well, honestly I would say, leaving out the PLUSes would drastically decrease readability.
[...]
Maybe people who grew up with other languages than Basic would be happier without pluses - people who grew up with Basic will not.
I absolutely agree.

Regards, Little John

Re: Macros for literal strings

Posted: Wed Oct 07, 2009 1:33 am
by Blue
Kaeru Gaman wrote:...
Maybe people who grew up with other languages than Basic would be happier without pluses - people who grew up with Basic will not.
Well put. I think you hit right on the nail with this !
That is really the main argument, both pro and con.

Re: Macros for literal strings

Posted: Wed Oct 07, 2009 8:42 am
by Kaeru Gaman
thank you.

and I'd like to put it as a clear con, because PureBasic in general followes classic Basic conventions,
except some few details like the brackets around the parameters.

Re: Macros for literal strings

Posted: Wed Oct 07, 2009 9:23 am
by Trond
Imagine a combination of six partstrings with five variables without pluses - sheer horror.
Only if you don't have syntax highlight.

Re: Macros for literal strings

Posted: Wed Oct 07, 2009 9:27 am
by Kaeru Gaman
Trond wrote:
Imagine a combination of six partstrings with five variables without pluses - sheer horror.
Only if you don't have syntax highlight.
I highlighted parts of my previous posting for you.

[edit]
put this in your IDE:

Code: Select all

diggledi.s = "abera"
wuddeldi.s = "cadabera"
howdeli.s  = "simsala"
doodeli.s  = "bimsala"
flanders.s = "sinobero"

out.s = "blawediblub " + diggledi + " hobbledipop " + wuddeldi + " snugglediwu " + howdeli + " madanedi " + doodeli + " dumpsedi " + flanders + " is stoopid"
syntax highlighting? where?

now eliminate the pluses...

Re: Macros for literal strings

Posted: Wed Oct 07, 2009 9:43 am
by PB
Man, I opened a can of worms with this request. :(

Re: Macros for literal strings

Posted: Wed Oct 07, 2009 11:02 am
by Trond
Kaeru Gaman wrote:
Trond wrote:
Imagine a combination of six partstrings with five variables without pluses - sheer horror.
Only if you don't have syntax highlight.
I highlighted parts of my previous posting for you.

now eliminate the pluses...
First of all, this was about string merging at compile time, with constants. You're using variables. Constants start with a # and use a different colour by default.
And I had to split the long line, but apart from that there's no problem:
Image
One of them is easier to read.