Please allow '(' in string operations

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Please allow '(' in string operations

Post by Little John »

"Classic" way of concatenating a string and a numeric expression:

Code: Select all

x = 7
res$ = "Result: " + Str(x + 2)
Debug res$                      ; -> 9
Since PB 5.10 we can do without Str(), but get a different result in this case:

Code: Select all

x = 7
res$ = "Result: " + x + 2
Debug res$                      ; -> 72
So please allow the following (which currently raises a syntax error):

Code: Select all

x = 7
res$ = "Result: " + (x + 2)
Debug res$                      ; -> 9
As Danilo wrote, this is also allowed in some other languages.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Please allow '(' in string operations

Post by freak »

quidquid Latine dictum sit altum videtur
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Please allow '(' in string operations

Post by jassing »

freak wrote:See also my comment there: http://www.purebasic.fr/english/viewtop ... 69#p406569
I'm with you -- Use Str() -- even tho I can use numbers and strings together, I prefer to use Str() still -- I find it more readable, and easier to verify what is being done. I hate auto-casting in general, as it leads to problems, like this...
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Please allow '(' in string operations

Post by BorisTheOld »

jassing wrote:I prefer to use Str() still -- I find it more readable, and easier to verify what is being done. I hate auto-casting in general, as it leads to problems, like this...
I agree completely. Auto-casting is a disaster waiting to happen.

I've learned the hard way that clarity should have precedence over brevity, and I happily use long variable names, lots of comments, and strict coding standards.

Non-rigorous coding practices are why most of today's software is bugridden rubbish. Check out most software forums and you'll find comments like, "For this month's release we fixed about 240 bugs" - as if that's something to be proud of.

Auto-casting, and similar "programming aids", should not be part of PB
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Please allow '(' in string operations

Post by IdeasVacuum »

clarity should have precedence over brevity, and I happily use long variable names, lots of comments, and strict coding standards
Agreed -a basic requirement to underpin good code and the autocasting simply isn't necessary at all in this case.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Please allow '(' in string operations

Post by rsts »

BorisTheOld wrote: Auto-casting, and similar "programming aids", should not be part of PB
+1
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Please allow '(' in string operations

Post by skywalk »

+1
The "cat is out of the bag", so this request seems to solve the issue of "x" + 1 + 2 <> "x" + (1+2).
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Please allow '(' in string operations

Post by jassing »

[sarcasm]Next some request will be for interpreting strings as numeric values so we can really have code that is hard to follow...[/sarcasm]
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Please allow '(' in string operations

Post by BorisTheOld »

And for those of you wondering why auto-casting is so bad, it's because the compiler will happily accept code that might otherwise be flagged as an error. And in such a situation the compiled code will most certainly not work correctly.

Unfortunately, the failure may not manifest itself until much later when the program is in production. Not only are such bugs very difficult to find, but the defective code might have been slowly destroying valuable data without showing any signs of failure. Or, worse case, it might be putting someone's life at risk.

That's why I said that auto-casting is a disaster waiting to happen. Far better to spend a little extra time writing bullet proof code, than have the compiler make assumptions about what you actually wrote.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please allow '(' in string operations

Post by Little John »

Some people wrote here that they don't like auto-casting in PB.
I'm aware of problems that auto-casting can cause, that's why I created the thread Cave when doing without Str()!.

There seems to be a misunderstanding: I did not request here to introduce auto-casting into PB, it is already there. E.g.

Code: Select all

x = 7
res$ = "Result: " + x + 2
does already work.

So why don't you make a separate feature request for disallowing this in PB?
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Please allow '(' in string operations

Post by BorisTheOld »

I know auto-casting is already in PB - I was just warning against the hazards of its existance.

My own coding practices are such that I won't run afoul of it. All our code is automatically created from application specifications, using templates that are known to generate valid code.

If the users of PB want to have auto-casting, then it's up to PB's authors whether such a feature is included. But if it were my choice I would get rid of it before too much damage is done.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please allow '(' in string operations

Post by Little John »

BorisTheOld wrote:I know auto-casting is already in PB - I was just warning against the hazards of its existance.
As I already pointed out, the purpose of this thread is not a general discussion about auto-casting.
Feel free to make a separate feature request for removing any auto-casting from PB.
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Please allow '(' in string operations

Post by BorisTheOld »

Little John wrote:As I already pointed out, the purpose of this thread is not a general discussion about auto-casting.
Seems to me that all of the above comments, not just mine, are saying that your suggestion is not such a good one.

Explaining why is, I hope, of use to others in deciding the merits of your proposal.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Please allow '(' in string operations

Post by Little John »

BorisTheOld wrote:Explaining why is, I hope, of use to others in deciding the merits of your proposal.
There wasn't much "explaining why" regarding anything about '(' in string operations. There mainly were comments against auto-casting in general.
And:
Little John wrote:I did not request here to introduce auto-casting into PB, it is already there.
Little John wrote:
BorisTheOld wrote:I know auto-casting is already in PB - I was just warning against the hazards of its existance.
As I already pointed out, the purpose of this thread is not a general discussion about auto-casting.
Actually shouldn't be too hard to understand.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Please allow '(' in string operations

Post by Tenaja »

+1 for little John's request.

If PB is going to allow the auto-cast, it should allow the parens.
Post Reply