yaml-spec icon indicating copy to clipboard operation
yaml-spec copied to clipboard

(?) YEP-1: Adding reserved name for anchor

Open sJakovac opened this issue 4 years ago • 2 comments

Not sure where proposals for new YAML features reside so I post this YEP (YAML enhancement proposal) here.

It would be useful to have an easier way of defining anchor with the same name as node key. I often find myself writing someKey: &someKey It's time/type consuming to write and a possible source of typos.

I propose a reserved name for the anchor which will denote that the name of the anchor will be the same as of the node key. Its alias will stay the same.

Some reserved name proposals:

  • _ like name for last returned value in Python's interactive shell
  • ? same as Bash shell variable holding exit status of last command

Usage in YAML would then be someKey: &_ or someKey: &?

sJakovac avatar Oct 03 '21 21:10 sJakovac

Thanks for contributing. We don't have a proper RFC process set up yet, so for now an issue is fine.

Some initial thoughts:

  • Both &_ and &? are currently valid anchor names. What would happen if there were an anchor *_ or *? in the document?
  • Currently, aliases and anchors are identified as the first step of composition, before tags are resolved and the canonical form of scalar content is available, so the anchor name would have to be based on the key node's formatted content.
  • What if the mapping key is not a scalar node? What if it's a scalar, but its formatted content is not a valid anchor name?

Thom1729 avatar Oct 07 '21 19:10 Thom1729

What about just allowing the anchor symbol to precede keys — so they key doubles as key and anchor — you can differentiate them from non-key anchors because they are followed by a :

&someKey:
  blah: 1

beorn avatar Dec 29 '23 00:12 beorn