Macros for literal strings

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Macros for literal strings

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Macros for literal strings

Post 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:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Macros for literal strings

Post 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.
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Macros for literal strings

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Macros for literal strings

Post 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.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Macros for literal strings

Post by Trond »

I know that they are merged when you use +. The purpose of leaving out the + was only to aid readability.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Macros for literal strings

Post by netmaestro »

I like the automatic string concatenation idea, that's a good feature request on its own.
BERESHEIT
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Macros for literal strings

Post 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.
oh... and have a nice day.
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Macros for literal strings

Post 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
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Macros for literal strings

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Macros for literal strings

Post 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.
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Macros for literal strings

Post by Trond »

Imagine a combination of six partstrings with five variables without pluses - sheer horror.
Only if you don't have syntax highlight.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Macros for literal strings

Post 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...
oh... and have a nice day.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Macros for literal strings

Post by PB »

Man, I opened a can of worms with this request. :(
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Macros for literal strings

Post 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.
Post Reply