bmx-ng / brl.mod

BlitzMax Runtime Libraries, for BlitzMax NG.
12 stars 11 forks source link

Brl.StringBuilder - as struct? #214

Open GWRon opened 3 years ago

GWRon commented 3 years ago

For now TStringBuilder is an object - but often you might just use it to have a "lightweight" string concatenation.

Maybe it would be useful to have it as a struct too?

HurryStarfish commented 3 years ago

What would that achieve?

GWRon commented 3 years ago

An object less which is just created "temporarily"

As said you might have a function in which you concatenate a string and return it. In this function you most probably do Local sb:TStringBuilder = new TStringBuilder.

But I understand that a struct + wrapper would mean a lot of boilerplate code ...

HurryStarfish commented 3 years ago

The whole point of a builder is being able to grow, so it needs to allocate heap memory. Turning a builder into a struct won't get rid of that. You'd save one single object allocation and end up with an "object" whose memory you need to destroy manually - which is what BlitzMax deliberately wants to avoid by having a garbage collector. You may as well call the underlying C function directly, but I don't really see why you'd want to do that tbh.

GWRon commented 3 years ago

Nonetheless it should be an object less..if only used as one shot builder in a function.

Surely too much efforts required (duplicate code) to make it a struct and a type (SStringBuilder and TStringBuilder).

Using the underlaying C stuff...would fight the approach of Blitzmax...a bit at least