[Proposal] Inline variable declarations in collections (and beyond?) #8334
Unanswered
NibblyPig
asked this question in
Language Ideas
Replies: 1 comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Imagine a script for a robot with that takes a
params Instruction []
, the order of the parameters is importantnew OrderRobot(new MoveForwardAction(5), new TurnLeftAction(), new ShootLaserAction());
This works fine, and is highly readable. You can always format it over multiple lines with indentation too.
But what if you want to manipulate one of these items later? I want to see if my laser action caused it to overheat, by checking the Overheated property.
What happens now is I have to write out my items in the wrong order:
With more items this could get a bit more complex, and when other developers jump in and need to extract other entries into variables there's no guarantee they'll take the time to add them nicely in the same order - they could just define the TurnLeftAction above the shootLaserAction! Or even at the top of the method above other code.
What if I could do this:
I expect this would be reserved for collections that use Add, similar to how we can use the { { x, y }, {a, b} } style notation to automatically invoke Add but if there are no limitations it could be expanded out further.
It could simply invisibly hoist the declaration above the statement for debugging purposes rather than anything more fancy.
This would just be a tool of course, it's gonna be messy to define massive single line Add or Params type calls and you probably shouldn't do that, but I think there are some use cases where you want to add a few items like this and it'd be nice to be able to retain a reference to one or more of them without having to restructure the code.
I expect this will come in most handy with fluent notation, where you want to refer back to something earlier:
Robot.Actions.Add(new Say("Goodbye World!"), var beeper = new BeepContinuously(), new Wait5Sections(), new Cancel(beeper), new Explode());
Since beeper has been hoisted there's no real issue with it being used within the same line.
Beta Was this translation helpful? Give feedback.
All reactions