EnableExplicit should require type declaration

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
sc4pb
User
User
Posts: 26
Joined: Tue Mar 07, 2023 5:33 pm

EnableExplicit should require type declaration

Post by sc4pb »

I understand that EnableExplicit forces the developer to define their variables:

Define foo

...but this is not a definition, there is no type, so it is assumed to be integer. This has caused me to accidentally define types as integers because of typos (forgetting the type)

I'd appreciate an option where EnableExplicit required both a variable name and type for every definition:

Define foo.i

Thanks
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: EnableExplicit should require type declaration

Post by jacdelad »

Hi sc4pb,
I don't think that's helpful. Integer is the most used variable type and from my point of view the programmer get's used to it very fast. So I think there's usually not much confusion. I think it would be more helpful to make EnableExplicit standard.

But let's see, what others think here.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: EnableExplicit should require type declaration

Post by luis »

It would be a nightmare.
And actually not having a type specified already gives you information about its type.
Integer it's the fundamental type, it's fine as it is IMO.
Imagine being forced to type .i when 80% of the time the current default is what you want. Why ?

AH, but I missed the fact you would like a further option for EnableExplicit, not change its current behaviour.
A stricter EnableExplicit then ?

I don't really think it's going to happen but as long as don't impact my existing code I don't care, just don't want to be forced to use it. Even if I'm not thrilled at the possibility of finding code using that option in the forum, I would have to change it back to make it fit with my code style.
"Have you tried turning it off and on again ?"
A little PureBasic review
sc4pb
User
User
Posts: 26
Joined: Tue Mar 07, 2023 5:33 pm

Re: EnableExplicit should require type declaration

Post by sc4pb »

Yes preserve existing EnableExplicit, just perhaps make a new keyword that would trigger explicit types as well.

Let's have different opinions, but I personally don't like the default type, whether integer or otherwise. And since (apparently) I didn't read the docs well enough early on, I was under impression that default type was long, not integer. This was just my mistake. But consider this example

Code: Select all

Define DateRightNow.q

DateRightNow=Date()
Debug FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss",DateRightNow)
The variable DateRightNow is a quad (8 bytes) and all is good. But now...

Code: Select all

Define DateRightNow

DateRightNow=Date()
Debug FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss",DateRightNow)
Exact same code, but a relative newcomer like me may not understand integer is being used. Worse, integer is 8 bytes on 64-bit machines, and 4 bytes on 32-bit machines. So this second example will fail after 2038 https://en.wikipedia.org/wiki/Year_2038_problem , but only on 32 bit machines, the 64 bit machines will be fine.

So for me personally, I like to declare my types and know ahead of time what trouble I'm getting into :)
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: EnableExplicit should require type declaration

Post by Paul »

sc4pb wrote: Thu Jun 08, 2023 3:29 pm Exact same code, but a relative newcomer like me may not understand integer is being used. Worse, integer is 8 bytes on 64-bit machines, and 4 bytes on 32-bit machines. So this second example will fail after 2038 https://en.wikipedia.org/wiki/Year_2038_problem , but only on 32 bit machines, the 64 bit machines will be fine.
Not a good example since PB Date command doesn't go past 2038 regardless of 32bit or 64bit OS :wink:
Image Image
sc4pb
User
User
Posts: 26
Joined: Tue Mar 07, 2023 5:33 pm

Re: EnableExplicit should require type declaration

Post by sc4pb »

Really? Well then PB should probably get a "DateQ" library out there ASAP because I'd rather be getting back 8 byte dates.

Anyway it was just an example. But it's also a mystery where other "handles" come from. When I get a handle back for a new network client connection, that handle is an integer. If I was (mistakenly) dropping that handle into a long, is there the possibility it will be garbled? Only if the handle on a 64 bit machine returned by network library is larger than the max value of a long. And I'm guessing it's not, but that's the point it's just a guess -- technically PB library can generate a handle value in any way it wants, and it'll be a mystery to us.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: EnableExplicit should require type declaration

Post by Fred »

Unless the doc say explicitly, all the return type are default type (integer):

Default type (ie: integer)
https://www.purebasic.com/documentation ... adget.html

A different type for return value:
https://www.purebasic.com/documentation/string/val.html

I would recommend to not strongly type it when using the default type, it was a mess when we went from 32-bit to 64-bit with all the code strongly using ".l" everywhere. I don't think we will ever change the ".i" as default but you better be on the safe side (and it's easier to read, as the default type can't be changed anymore in PB, so it's always the good one to store default type results).

And yes, if you put an handle 64-bit into a long, chances are very high it will be truncated because Windows now uses randomly memory allocation to be unpredictable to buffer overflow attacks
Post Reply