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

Into the Box Round 1 of Sessions and Workshops are now out!

Into the Box Round 1 of Sessions and Workshops are now out!

Our first round of sessions and workshops for Into the Box 2025 is here! Get ready to dive into a world of modern web development with hands-on workshops and engaging sessions led by Ortus Solutions and Community CFML and BoxLang experts. Visit intothebox.org to explore what’s in store—this is just the beginning, with much more content coming soon!

Maria Jose Herrera
Maria Jose Herrera
January 20, 2025
BoxLang 1.0.0 Beta 26 Launched

BoxLang 1.0.0 Beta 26 Launched

We’re thrilled to announce the release of BoxLang 1.0.0 Beta 26, a monumental update that takes performance and functionality to the next level. This beta officially certifies the ColdBox HMVC Framework to run on BoxLang, marking a significant milestone in compatibility. Not only can you now run all ColdBox applications seamlessly on BoxLang, but with the latest ColdBox snapshot, you can also build your entire applications in BoxLang, unlocking the full potential of this dynamic and expressive language for modern application development.

Luis Majano
Luis Majano
January 20, 2025