Toml: Suggestion: Add support for ranges

Created on 20 Aug 2016  路  4Comments  路  Source: toml-lang/toml

Proposal

It would be extremely convenient to allow numeric arrays to be specified with a range and step. I propose the following syntactic sugar

  • Range: [start...end]
  • Range with custom step: [start:end[:step]]

Ranges can be specified as an element within an array or as a _bare_ range. Bare ranges are specified without brackets.

It may also be handy to permit date types as the start and end value.

Examples

1...3 -> [1, 2, 3]
3...1 -> [3, 2, 1]
[1...3, 10...15] -> [1, 2, 3, 10, 11, 12, 13, 14, 15]
[1:3] -> [1, 2, 3]
[2:6:2] -> [2, 4, 6]
[1:0:-.2] -> [1.0, 0.8, 0.6, 0.4, 0.2, 0.0]

Motivation

Creating an array of a large sequence can be tedious and error-prone to type in. Programming languages geared towards numeric processing like Python, Matlab, and Julia offer built in range operators which are VERY useful.

Possible use cases include:

  • Specifying a range of ports in a configuration file to block
  • Creating a sequence of times to run a process at (ie, run every 30 minutes for the first 12 hours, and then every 120 minutes for the next 12 hours [0:760:30, 761:1440:120])

Most helpful comment

@jdfergason if you want a one-liner, you could have a spec like this as well

value_min_max_step = [10, 300, 5]

All 4 comments

Three counter-arguments:

  • TOML isn't a programming language but a minimal configuration format. And this feature, in my opinion, isn't minimal.
  • Supporting ranges will probably make the parsers slower and more complex.
  • It might be useful, but YAML and JSON don't support it and it doesn't seem to be a big problem, so I doubt that it's necessary.

@TheElectronWill I understand your concerns about the language becoming overly complicated; but I find minimal to be a somewhat arbitrary distinction. The most minimal format would only allow a single value per line. No types, no key names, no nested tables or arrays of tables, no nothing.

I think that adding ranges would enhance the clarity of TOML files and be generally useful in configuration settings. To me, declaring an array as:

key = [8001...8010]

is much easier to read and write than:

key = [8001,8002,8003,8004,8005,8006,8007,8008,8009,8010]

As the number of values increase in the array the better chance that I accidentally forget a comma by accident.

I tested this feature out in my own implementation of TOML and it only added 4 lines of additional code and no meaningful degradation of parse performance (at least as instruments measures it in Swift)

I work in the scientific community and a feature like this would be tremendously valuable because we are always creating configuration files that control how complex simulations are run. These configurations frequently require the specification of ranges of values. We can just model this as three keys:

value_start = 10
value_end = 300
value_step = 5

and do the parsing in application code but I find the proposed syntax to be a lot simpler.

@jdfergason if you want a one-liner, you could have a spec like this as well

value_min_max_step = [10, 300, 5]

One way to think about minimalism is to see if something can already be accomplished with the current feature set without undue consternation. In this case, ranges can be stated explicitly in an array, or in cases where applications expect a lot of large range scenarios, they can accept something like this:

key1 = 8000 # single value
key2 = [8000, 8001] # multiple values in standard array
key3 = { min = 8000, max = 9000 } # range spec using inline tables, default step = 1 
key4 = { min = 8000, max = 9000, step = 10 } # range with custom step

As such, I think ranges are overly complex for TOML's current flavor of minimalism.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ChristianSi picture ChristianSi  路  3Comments

genericptr picture genericptr  路  4Comments

Silentdoer picture Silentdoer  路  4Comments

jakwings picture jakwings  路  3Comments

tamasfe picture tamasfe  路  3Comments