tweag / nickel

Better configuration for less
https://nickel-lang.org/
MIT License
2.23k stars 85 forks source link

A way to assign a variable to a field with the same name #1931

Open norpadon opened 1 month ago

norpadon commented 1 month ago

Is your feature request related to a problem? Please describe. Here is an example config:

let learning_rate = 1e-3
in {
    optimizer = {
        learning_rate = learning_rate
    }
}

If I try to compile it, I get an Error:

error: infinite recursion
  ┌─ /Users/norpadon/Work/ticksim-ml/nuggets/configs/demo.ncl:4:25
  │
4 │         learning_rate = learning_rate
  │                         ^^^^^^^^^^^^^ recursive reference

As I understand, the only way to fix it is to rename the outer learning_rate variable, which is inconvinient.

Describe the solution you'd like I see two possible solutions: 1) Introduce some king of special outer record which always contains bindings in the outer scope, so that I can write {learning_rate = outer.learning_rate} 2) Disabling recursion for quoted field names, e.g. {"learning_rate" = learning_rate}

yannham commented 1 month ago

Hi,

This is indeed missing. For the record, I think this is a duplicate of https://github.com/tweag/nickel/issues/747, which already has some proposals. But we can maybe keep this issue open as well, because the original one isn't as searchable.