Mixing strings and integer !?!?!

Just starting out? Need help? Post your questions and find answers here.
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Mixing strings and integer !?!?!

Post by StarBootics »

Hello everyone;

Is this following code normal :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Something weird about string
; File Name : Mixing strings and integer.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 19-05-2020
; Last Update : 19-05-2020
; PureBasic code : V5.72
; Platform : Linux
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;
; Is this a regression or a normal behaviour
; Mixing string and integers without compiler
; complaining about different types of vars !?!?!
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Define pointer.l

MyString00.s = "Something " + *pointer + " else "
MyString01.s = " Something " + pointer + " else "

pointer = 1

MyString03.s = MyString00 + pointer + MyString01 

Debug MyString00
Debug MyString01
Debug MyString03

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Something 0 else
Something 0 else
Something 0 else 1 Something 0 else
From my perspective this code shouldn't work at all but it does.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
breeze4me
Enthusiast
Enthusiast
Posts: 527
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Mixing strings and integer !?!?!

Post by breeze4me »

It is a feature added quite a long time ago.
14th February 2013 : Version 5.10
- Added: autocast of numeric values when string are involved, allowing to concatenate string and numeric in constants
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Mixing strings and integer !?!?!

Post by netmaestro »

I must confess I've never been able to bring myself to trust it, I always use Str(). Something just seems not right about being able to code like that. Probably just me, I'm happy it's there for those who like it, which is likely most coders.
BERESHEIT
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Mixing strings and integer !?!?!

Post by StarBootics »

netmaestro wrote:I must confess I've never been able to bring myself to trust it, I always use Str(). Something just seems not right about being able to code like that.
In my case that lead to an unexpected result due to a typo in my code.

Anyway, I will never use this feature because it appear very odd to me.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Mixing strings and integer !?!?!

Post by infratec »

netmaestro wrote:I must confess I've never been able to bring myself to trust it, I always use Str(). Something just seems not right about being able to code like that. Probably just me, I'm happy it's there for those who like it, which is likely most coders.
+1
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Mixing strings and integer !?!?!

Post by Little John »

User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Mixing strings and integer !?!?!

Post by Demivec »

It is documented on the Help page for Variable Types and Operators, under the '+'sign.

Code: Select all

With strings the '+' is also valid for combining the contents of two strings, where the result will be assigned to the string on the LHS with the '=' operator or will be directly stored into the string on the LHS. Numeric values are also accepted for combination with a string. It will behave like using Str(), Str() or StrD() with their defaults for the optional parameters.

Example:
a$ = b$ + " more" ; Combines the content of the string "b$" with the string " more" and save this into the string "a$"
a$ + b$ ; Attach the content of the string b$ directly to the string a$. a$ = b$ + 123
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Mixing strings and integer !?!?!

Post by HeX0R »

StarBootics wrote:
netmaestro wrote:I must confess I've never been able to bring myself to trust it, I always use Str(). Something just seems not right about being able to code like that.
[...]Anyway, I will never use this feature because it appear very odd to me.
Yeah, it looks and feels like using a scripting language, I only use this for quick debug phrases.
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Mixing strings and integer !?!?!

Post by NicTheQuick »

I like it a lot. But it would be even cooler if this would work:

Code: Select all

Define a.i = 5

Debug "Double: " + (a * 2)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Mixing strings and integer !?!?!

Post by Marc56us »

Just remember that if it starts with a string (even an empty one) everything that follows is considered as text EXCEPT for explicit mention (by using a function)

Code: Select all

a = 25

; Debug a + " item"             ; error
Debug "" + a + " items"         ; 25 items

Debug "" + Sqr(a) + " items"    ; 5 items  

Debug "" + a + a + " items"     ; 2525 items
Debug "" + 25 + 25 + " items"   ; 2525 items

; Debug "" + (a+a) + " items"   ; '(' are Not allowed in string operations.

Debug "" + Str(a+a) + " items"  ; 50 items
Easy.
:wink:
Post Reply