Blog

Introducing numeric placeholders in BoxLang source!

Maria Jose Herrera September 18, 2024

Spread the word

Maria Jose Herrera

September 18, 2024

Spread the word


Share your thoughts

Introducing Numeric Placeholders in BoxLang

We're excited to announce a new addition to our BoxLang parser—a feature that draws inspiration from several other languages but fits naturally within BoxLang. The conversation around which features from other languages make sense to include in BoxLang is ongoing. Just because a feature works in language X doesn't mean it will serve a purpose or feel idiomatic in BoxLang. However, in this case, the utility and readability benefits are clear.

What Are Numeric Placeholders?

Numeric placeholders allow you to insert underscore characters (_) inside numeric literals to make large numbers more readable. For example, consider this number:

n = 1000000000

Is that 1 billion? Or 100 million? It takes a moment to decipher. With numeric placeholders, your code becomes more readable:

n = 1_000_000_000

Ah, it's 1 billion! Much clearer, right? There's no strict rule on where you can place the underscores, as long as they are inside the number and not leading or trailing. Even this, though a bit excessive, is valid:

n = 1_0_0_0_0_0_0_0_0_0

Where Can You Use Numeric Placeholders?

Numeric placeholders aren't just limited to integers. You can also use them in:

  • Decimals:

    n = 3.141_592_653_59
    
  • Scientific notation:

    n = 1e2_345
    

These underscores are purely for readability in your source code. At compile time, they are simply removed, meaning they don't affect the bytecode and won't appear anywhere in your running application.

Borrowed From the Best

This feature isn't entirely new to the programming world. It’s borrowed from languages like Ruby, Java, Swift, Kotlin, and JavaScript, where it has proven to be a valuable tool for writing more readable code.

Ruby Example:

puts 1_000_000 * 9
# Outputs: 9000000

This is functionally identical to:

puts 1000000 * 9
# Outputs: 9000000

But the first example is much more readable and developer-friendly.

JavaScript Example:

1_000_000 * 9;
// Outputs: 9000000

How It Works in BoxLang

Here are some examples of valid uses of numeric placeholders in BoxLang:

result1 = 5_000         // 5000
result2 = 5_000.000_4   // 5000.0004
result3 = .1_2          // .12
result4 = 1.2_3         // 1.23
result5 = 1_2.3_4e5_6   // 12.34e56
result6 = 1_2_3_4_5_6_8 // 1234568

You can even use numbers as struct keys or in dot notation:

str = {
    1_0 : "brad"
}
result7 = str[ 10 ]     // "brad"
result8 = str.1_0       // "brad"

str2 = {
    '1_0' : "brad"
}
result9 = str2[ '1_0' ] // "brad"

What’s Not Allowed

There are a few invalid cases where an exception will be thrown:

result1 = _5  // Invalid: variable _5 not found
result2 = 5_  // Invalid: Identifier name cannot start with a number

Since underscores can be valid identifiers in BoxLang, the parser might interpret _5 as a variable rather than a numeric literal.

Conclusion

We hope this new feature will make your code easier to read and maintain. Numeric placeholders are a small but meaningful addition that aligns with BoxLang's goals of improving developer productivity and code clarity. As always, we're open to your feedback as we continue to enhance BoxLang with features that truly make a difference.

Add Your Comment

Recent Entries

Adobe CFSummit 2024 Recap | Ortus Solutions

Adobe CFSummit 2024 Recap | Ortus Solutions

A huge thank you to everyone who visited our booth! We had the pleasure of engaging in meaningful conversations about Modern CFML and ColdFusion development, sharing insights, and exploring exciting new ideas. Visitors also got a chance to participate in some fun activities and win fabulous prizes, including our customized t-shirts, which were a huge hit!

Maria Jose Herrera
Maria Jose Herrera
October 02, 2024
BoxLang September Newsletter

BoxLang September Newsletter

Welcome to the September edition of the BoxLang Newsletter! This month has been packed with exciting updates, new features, and enhancements that continue to shape BoxLang into a robust and versatile programming language. In this edition, we highlight the latest beta releases, new integrations, and key features that have been introduced.

Maria Jose Herrera
Maria Jose Herrera
October 01, 2024
Fall Savings: Unlock Exclusive Discounts Until October 31st!

Fall Savings: Unlock Exclusive Discounts Until October 31st!

Get ready for the Ortus October Fall Savings! This is your opportunity to elevate your development experience with fantastic offers designed for everyone—from seasoned developers to tech enthusiasts. Our special promotions are here to help you achieve your goals while saving money!

Don’t Miss Out on These Amazing Offers!

Use the code FALLSAVINGS24 at checkout to enjoy these incredible discounts before they expire on October 31st

Cristobal Escobar
Cristobal Escobar
October 01, 2024