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

ColdBox Training: From Hero to Superhero – Master ColdFusion and BoxLang Development

ColdBox Training: From Hero to Superhero – Master ColdFusion and BoxLang Development

Are you ready to take your ColdBox development skills for ColdFusion and BoxLang to the next level? Whether you're new to ColdBox or looking to sharpen your expertise, our ColdBox From Hero to Superhero training is the perfect opportunity to become a ColdFusion and BoxLang powerhouse.

Why Attend?

This hands-on training is designed to help ColdFusion and BoxLang developers level up their ...

Cristobal Escobar
Cristobal Escobar
February 19, 2025
Meet Raymond Camden: BoxLang Advocate!

Meet Raymond Camden: BoxLang Advocate!

We’re thrilled to announce that Raymond Camden, a leading voice in the CFML community, is joining us as a BoxLang Advocate. Currently collaborating with us as a contractor, Raymond brings years of web development experience and a passion for simplifying complex technologies. His expertise makes him the perfect advocate to explore BoxLang, a modern, CFML-compatible programming language.

Maria Jose Herrera
Maria Jose Herrera
February 19, 2025
Meet BoxLang at DevNexus 2025!

Meet BoxLang at DevNexus 2025!

We’re thrilled to announce that Ortus Solutions will be a Silver Sponsor at DevNexus 2025, one of the largest and most exciting Java conferences in the world! Our team will be on-site, spreading the word about BoxLang and its latest advancements, designed specifically to empower Java developers with dynamic, modular, and productive development capabilities.

Maria Jose Herrera
Maria Jose Herrera
February 19, 2025