Allow underscore as separator for number literals

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Allow underscore as separator for number literals

Post by Josh »

Lord wrote:Even if I don't like number seperators,
there are some characters left: ^°´`
for separetion.
° and ´ are no Ascii characters

BarryG wrote:I don't think there's any point to this request anyway, because when you copy source codes from somewhere else (C++, StackOverflow, etc) then the separators aren't going to be there anyway. Are you going to manually change every number from the copied code?
No one said you have to use the separators.

TI-994A wrote:A prefix should be implemented to indicate such formatting, similar to the tilde (~) for escaping strings. Best to keep literals and formatting separate.
Contrary to the request here, the tilde character is required for an escaped string.

The requested delimiters have nothing to do with formatting the literal, the compiler would simply ignore the delimiters. The delimiters are only for better readability of the code, in case of an output no delimiter would be present either.




In general, I would say that there is no need to reinvent the wheel. It is not a shame to look beyond the end of the own nose and then one will quickly discover that alternativ used separators _ or ' are quite common in other programming languages.
sorry for my bad english
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: Allow underscore as separator for number literals

Post by BarryG »

Josh wrote:No one said you have to use the separators.
I'm quite obviously referring to people who do want to use them, but it does also directly affect others who don't (see my last comment below).

Say they copied this (cut-down) StackOverflow code; are they really going to sit there and insert underscores in every blue number because they prefer underscores? Or will they leave it as-is and only underscore their own numbers that they specifically typed? It just seems to me that this request is just making things harder on themselves... unless they never intend to copy anyone else's code - ever.

And then if I copy someone's code here who uses underscores, I have to go and strip them all out (or put up with them). Yuck; thanks for the extra work and eyesore code.
// Generate Data
int arraySize = 32768;
int Data[] = new int[arraySize];

For (int i = 0; i < 100000; ++i)
// Primary loop
// (Cut out for this example)
System.out.println((System.nanoTime() - start) / 1000000000.0);
System.out.println("sum = " + sum);
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Allow underscore as separator for number literals

Post by TI-994A »

Josh wrote:The requested delimiters have nothing to do with formatting the literal, the compiler would simply ignore the delimiters. The delimiters are only for better readability of the code, in case of an output no delimiter would be present either.
One of the proposed characters for such a delimiter is the underscore, which is ubiquitous in variable naming conventions. This could easily cause visual ambiguity. A prefix would provide a clear and discernable indication.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Allow underscore as separator for number literals

Post by Josh »

I don't like the underscore for purely visual reasons, which is why I suggested the single quote.

Advantages of the single quote:
- Better optics (at least in my eyes)
- Also used in pocket calculators
- Also used as thousands separator in Switzerland
- Used by C++

Disadvantages of the single quote:
- Ide would have to be adapted additionally


But when I look at when the last wish was implemented, this all is just a theoretical discussion anyway :mrgreen:
sorry for my bad english
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Allow underscore as separator for number literals

Post by skywalk »

It is interesting how "one size fits all" does not apply in this case.
Since this is an optional feature request, we can only comment.
I hate to see bug fixes delayed or new bugs emerge in the IDE as a result.
I deal with numbers of all shapes and sizes every day and I would not use this feature.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Allow underscore as separator for number literals

Post by Danilo »

Underscore is used as digit separator in many languages:

- TypeScript
- JavaScript (ES2021), ES2021: numeric separators
- Java
- C# 7 Digit Separator
- Python: Underscores in Numeric Literals
- Ruby
- Ada

- D
Integers can have embedded ‘_’ characters to improve readability, and which are ignored.

Code: Select all

20_000        // leagues under the sea
867_5309      // number on the wall
1_522_000     // thrust of F1 engine (lbf sea level)
- Rust (see also: Rust by Example: Literals and operators)
All number literals allow _ as a visual separator
- Julia
The underscore _ can be used as digit separator:
julia> 10_000, 0.000_000_005, 0xdead_beef, 0b1011_0010
(10000, 5.0e-9, 0xdeadbeef, 0xb2)
- Swift (scroll down to "Integer Literals" and "Floating-Point Literals")
Underscores (_) are allowed between digits for readability, but they’re ignored and therefore don’t affect the value of the literal.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Allow underscore as separator for number literals

Post by Sicro »

BarryG wrote: Tue Feb 23, 2021 4:06 pmSay they copied this (cut-down) StackOverflow code; are they really going to sit there and insert underscores in every blue number because they prefer underscores?
Yes, the adopted code snippet is mostly edited anyway, because of different code style, adding comments etc. In addition, it's a task you'll only do once, but will benefit from better code readability again and again in the future.

By the way, code snippets from StackOverflow etc. should rather be considered as example codes that you rewrite in your own way. This avoids that you have to take over the license under which the codes published there are licensed. Also, you'll learn more if you don't just copy code.
BarryG wrote: Tue Feb 23, 2021 4:06 pmOr will they leave it as-is and only underscore their own numbers that they specifically typed?
Yes, I don't rewrite third-party module codes to my code style, I just include it in my code with XIncludeFile. Of course, I read the module codes before, if they are not from safe sources.
BarryG wrote: Tue Feb 23, 2021 4:06 pmAnd then if I copy someone's code here who uses underscores, I have to go and strip them all out (or put up with them). Yuck; thanks for the extra work and eyesore code.
You already have a similar problem if the copied code has a different code style than your own.

In a team work, team members agree on a consistent code style, but they certainly won't keep rewriting third-party code to their own code style; instead, they'll outsource it to a separate code file and leave the code style as is. This also avoids continuous code style adjustments when the third-party developer keeps updating his code.

If the native PB libraries (Gadget, Window, Network, etc.) had been implemented as PBI code files, you also would surely not rewrite each PBI file according to your code style, but simply include them in your code.
skywalk wrote: Tue Feb 23, 2021 6:47 pmI hate to see bug fixes delayed or new bugs emerge in the IDE as a result.
The PB team prioritizes bug fixes and feature implementations. Feature request creators cannot be held responsible for delayed bug fixes.

I also see bug fixes as more important, but new features are also important for a programming language to remain useful in the future.
Josh wrote: Tue Feb 23, 2021 5:54 pmBut when I look at when the last wish was implemented, this all is just a theoretical discussion anyway :mrgreen:
I'm also not waiting for it, but it would be nice to see it implemented to make the PB programming language a little more like modern programming languages.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply