mix strings with numerical values

Just starting out? Need help? Post your questions and find answers here.
FlatEarth

mix strings with numerical values

Post by FlatEarth »

Hi
Why can't we mix numerical and then string values in order? this feature is available in other languages like lua.
Has pb team made this feature incomplete? Or does it follow a certain rule?

Code: Select all

Define a = 2
Define name.s = "MyText"
Define result.s

;result = a+name
;Debug result

result = name+a+a
Debug result

User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: mix strings with numerical values

Post by Danilo »

Without that check, it wouldn‘t be possible to catch any errors when converting from number types to strings. You could use numbers everywhere and if a parameter is of type string, the number would automatically be converted to a string. It may not always be what you want. ;)

Code: Select all

LoadFont(1,2,3,4)

SetWindowTitle(0,1)

OpenWindow(#PB_Any, 0,0,800,600,0) ; No window title?

StringGadget(1, 0, 0, 100, 20, #PB_String_Numeric)
ButtonGadget(2, 0, 40, 100, 20, #PB_Button_Toggle) ; Where is the button text?

A$ = 123
The feature makes sense for the good example that you have in mind,
but at the same time it opens doors for many error possibilities that you don‘t want.

Most of the time you don‘t want to write:

Code: Select all

result.s
...
result = 12 + „hello“
result = 1 + 2  ; results in „12“
FlatEarth

Re: mix strings with numerical values

Post by FlatEarth »

@Danilo : Thank you for your answer.
I didn't ask anything strange :D according to your answer, we don't even need the following feature :

Code: Select all

Define a = 2
Define name.s = "MyText"
Define result.s

result = name+a+a
Debug result
because we can write it this way

Code: Select all

result = name+Str(a)+Str(a)
I do not want to be out general principles of programming,I don't want to use strings instead of numbers and numbers instead of strings.
but what is wrong with mix numbers and then strings?
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: mix strings with numerical values

Post by Danilo »

You didn’t ask something strange, because this is the foundation of programming language systems.

There are already tons of informations available for learning about programming languages,
programming language design, type systems, etc. — it is important stuff you need to know.

- https://thecodeboss.dev/2015/11/programming-concepts-static-vs-dynamic-type-checking/
- https://en.wikipedia.org/wiki/Type_system
- https://en.wikipedia.org/wiki/Type_safety

Pierce, B: Types and Programming Languages (Mit Press)
- https://www.amazon.com/dp/0262162091/

FlatEarth wrote:because we can write it this way

Code: Select all

result = name+Str(a)+Str(a)
Yes, in the early days of PureBasic only this was allowed.

The shortcut to allow concatenation of numbers to strings was added later, at the premise that the string comes first.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: mix strings with numerical values

Post by Josh »

Use a "" as first part:

Code: Select all

Define a = 2
Define name.s = "MyText"
Define result.s

result = ""+a+name
Debug result

result = name+a+a
Debug result
sorry for my bad english
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: mix strings with numerical values

Post by Josh »

Danilo wrote:Without that check, it wouldn‘t be possible to catch any errors when converting from number types to strings. You could use numbers everywhere and if a parameter is of type string, the number would automatically be converted to a string. It may not always be what you want. ;)

Code: Select all

LoadFont(1,2,3,4)

SetWindowTitle(0,1)

OpenWindow(#PB_Any, 0,0,800,600,0) ; No window title?

StringGadget(1, 0, 0, 100, 20, #PB_String_Numeric)
ButtonGadget(2, 0, 40, 100, 20, #PB_Button_Toggle) ; Where is the button text?
That's not quite right Danilo, this is not the way an eval system should work. If done correctly, each parameter is resolved individually, and this is completely independent of what comes before this parameter, what comes after this parameter and what data type is expected for this parameter. So no parameter is automatically converted to a string if no string is involved in the subterm of the parameter. Each of the functions shown by you would immediately end in an compilation error.
Danilo wrote:

Code: Select all

result.s
result = 1 + 2  ; results in „12“
Same here. If done correctly, the result would be a string with the value "3". The assignment operator has a very low priority and so the rhs-term is resolved first whereby it doesn't matter what kind of datatype the result variable is. Only when the rhs term is completely resolved the result is converted into the data type of the target variable and this result is assigned.



I don't want to say that it would be useful to integrate the functions suggested by FlatEarth into Pb this way (I'm not entirely sure about this myself), but it would be possible in any case. Additionally it should be mentioned that the evaluation functions of Pb, let's say it friendly, are very, very special and make many things much more complicated than in other programming languages.
sorry for my bad english
Post Reply