Provide a way to define simple defaults regardless of target language #345
Labels
design and planning
Various discussions about the future
enhancement
feature request
Big and small feature requests
Instead of this:
We want this:
or maybe even use a dedicated syntax for it such as
Why wasn't it done before?
There was the need to use default values that are defined in some code outside of ATD. For example, in OCaml, we would pull the default value from another module using
My_defaults.default_answer
. This is an OCaml expression and we would need the equivalent for Python.It would have been possible to create a language for constructing values within ATD. It would also have been possible to create a module system for ATD. Both of these things were considered extra complexity and extra work at the time (12 years ago) and it hasn't been revisited since.
What would it take?
42
but not40 + 2
).let default_answer = 42
. This would be useful for sharing defaults across various fields, types, or modules.Who would benefit from this?
Any multi-language use of ATD, as it was originally intended, as opposed to using it for only one language (typically OCaml) or maybe two.
Syntax proposal
Default field values:
The
~
could be optional since the=
syntax implies a default, giving usNamed constants:
Implementation issues
The particular representation of an ATD type is up to the target language and may be affected by ATD annotations. Therefore, a value specified as
42
in ATD would be inferred as an ATDint
but could translate to a JavaScript number (a 64-bit float), a Python int of unlimited precision, or a fixed-width twos-complement int (32/64 bits in many languages, 31/63 bits in OCaml). The ATD parser and code generator should settle on a representation that works for all target languages:We should assume a module system based on the current proposal, in which an ATD module is translated without having access to other ATD modules. This means that a constant declaration
let default_answer = 42
needs to be either restricted to local use (not visible from other modules :-( ) or expressed in the correct format (e.g. an OCaml int64 literal if an annotation calls for it:let answer = 42L
). This implies that format annotations such as<ocaml repr="int64">
can only be applied on thelet
definition:This would not work:
The text was updated successfully, but these errors were encountered: