It is currently Sun May 31, 2020 2:01 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: mix strings with numerical values
PostPosted: Fri May 22, 2020 12:04 pm 
Offline
User
User
User avatar

Joined: Tue Jan 28, 2020 10:17 am
Posts: 53
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:
Define a = 2
Define name.s = "MyText"
Define result.s

;result = a+name
;Debug result

result = name+a+a
Debug result


_________________
FlatEarth addresses in PB :
Engine3D --> SkyDome
Mesh --> CreatePlane


Top
 Profile  
Reply with quote  
 Post subject: Re: mix strings with numerical values
PostPosted: Fri May 22, 2020 12:36 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2992
Location: Planet Earth
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:
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:
result.s
...
result = 12 + „hello“
result = 1 + 2  ; results in „12“


Top
 Profile  
Reply with quote  
 Post subject: Re: mix strings with numerical values
PostPosted: Fri May 22, 2020 1:38 pm 
Offline
User
User
User avatar

Joined: Tue Jan 28, 2020 10:17 am
Posts: 53
@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:
Define a = 2
Define name.s = "MyText"
Define result.s

result = name+a+a
Debug result


because we can write it this way

Code:
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?

_________________
FlatEarth addresses in PB :
Engine3D --> SkyDome
Mesh --> CreatePlane


Top
 Profile  
Reply with quote  
 Post subject: Re: mix strings with numerical values
PostPosted: Fri May 22, 2020 2:11 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2992
Location: Planet Earth
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:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: mix strings with numerical values
PostPosted: Fri May 22, 2020 5:04 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1074
Use a "" as first part:
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: mix strings with numerical values
PostPosted: Fri May 22, 2020 10:01 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 1074
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:
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:
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye