In addition to the good sschaef solution, I want to mention a few features that are commonly used to get rid of the commas in building lists for DSL.
Colons
It may be trivial, but sometimes it is ignored as a solution.
line1 :: line2 :: line3 :: Nil
DSL often requires that each line containing some instructions / data be completed the same way (against lists where everyone except the last line will receive a comma). With such solutions, replacing lines can no longer ruin the trailing comma. Unfortunately, Nil looks a little ugly.
Fluid API
Another alternative that might be interesting for DSL is something like this:
BuildDefinition() .line1 .line2 .line3 .build
where each line is a member function of the builder (and returns a modified builder). This solution requires you to eventually convert the builder to a list (which can be performed as an implicit conversion). Note that for some APIs it may be possible to bypass the linker instances themselves and retrieve data only where necessary.
Constructor API
Similarly, another possibility is to use constructors.
new BuildInterface { line1 line2 line3 }
Here BuildInterface is a sign, and we just create an anonymous class from the interface. Line functions call some member functions of this characteristic. Each call can internally update the state of the assembly interface. Note that this usually results in a volatile design (but only during construction). To retrieve a list, you can use implicit conversion.
Since I donβt understand the actual purpose of your DSL, Iβm not sure if any of these methods are interesting for your scenario. I just wanted to add them, as they are the usual ways to get rid of ",".
source share