Bug or intended behavior?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Bug or intended behavior?

Post by Quin »

This code:

Code: Select all

EnableExplicit

Define.i a = 12, b = 42, c$ = "test"
Debug a
Debug b
Debug c$
works without an error. I personally expected this to give a compile-time erorr because I'm declaring a string in a line of a bunch of integers, but if this was intentional it's actually convenient for functions where I declare a bunch of local ints and then just one or two strings. Hence, I'm curious if this is intentional or not?
Thanks.
infratec
Always Here
Always Here
Posts: 7616
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Bug or intended behavior?

Post by infratec »

It looks that you 'define' the default type, but you can override it.

Btw. in my opinion this is a bad coding style.

Code: Select all

EnableExplicit

Define.u a = 12, b = 42, c$ = "test", c.i = 100000
Debug a
Debug b
Debug c$
Debug c

a = 100000
Debug a
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Bug or intended behavior?

Post by Quin »

Oh you're right, it works with any type, not just $. You can override the default type for Define/Protected. I didn't know that, from what I remember reading the docs years ago, the default type can't be overridden, but obviously I'm wrong. Just goes to show that it doesn't hurt to revisit things like this every once in a while.
Also I agree with you about it generally being bad practice, but there are some times when it's necessary.
User avatar
Demivec
Addict
Addict
Posts: 4267
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Bug or intended behavior?

Post by Demivec »

Quin wrote: Sat May 03, 2025 9:34 pm Oh you're right, it works with any type, not just $. You can override the default type for Define/Protected. I didn't know that, from what I remember reading the docs years ago, the default type can't be overridden, but obviously I'm wrong. Just goes to show that it doesn't hurt to revisit things like this every once in a while.
Also I agree with you about it generally being bad practice, but there are some times when it's necessary.
An unspecified type is one one in which there is no type specified using the period or dot notation. The default type for variables with unspecified types is '.i'. In PureBasic's past, the keyword 'Define' used by itself with the dot notation (i.e. Define.c) allowed changing the default type for all future unspecified types.

Things were later changed to limit the default to each individual use of 'Define' instead of all future ones. It was always permitted to specify something different than the default fir each individual variable declared in the Define statement.

Even so, as infratec said it may not show good form to specify groups of variables of the same type innermixed with variables of other types. Along with the possibility of specifying a variable's initial value those things are a matter of personal style and preference. Some coding styles want to combine everything in one statement while others want only variables of ore type in a Define or for things to be easily understandable at a glance when reviewing or debugging the code later and still other styles want to avoid declaring initial values in the Define statement.
Post Reply