#2727 StrBuf Add Operator

SlimerDude Sun 13 Jan

I can't find the topic, but someone once asked how to create / initialise a StrBuf with a Str. The answer is to use buf := StrBuf().add("-my-str-") but...

If the @Operator facet were to be added to StrBuf.add() then that would lend itself to a more idiomatic Fantom statement of:

buf := StrBuf { "-my-str-", }

which works better for multi-line initialisations:

buf := StrBuf {
  "my
   multi
   line
   string",
}

What of it?

andy Mon 14 Jan

FWIW -- a data point from a similar scenario:

I explored that method in an internal Web library I had -- essentially this:

a := Elem("h1") { "This is the title", }
b := Elem("h1") { it.text = "This is the title" }

Initially was nice using the first method, but over time I found the code a bit obtuse and reverted back to using the traditional assignment operator instead.

brian Mon 14 Jan

I don't personally think allowing comma operator on StrBuf makes sense because its not really a collection of items like a List, Elem children, etc. For example StrBuf.add doesn't really add Str as a new item to the StrBuf. The original Str added is no longer a discrete thing, but more like a mathematical add operation where I'm creating something new.

Login or Signup to reply.