umka-lang icon indicating copy to clipboard operation
umka-lang copied to clipboard

format function that just returns a string

Open ske2004 opened this issue 3 years ago • 4 comments

The language provides sprintf but it requires a whole load of tax work to actually use because of it's similarity to C. With Umka a better formatting function can be made that simply returns an already formatted string to you. The API is format("%d", 123)

ske2004 avatar Jul 21 '22 20:07 ske2004

What is the exact difference between sprintf() and format()? Returning the string as the function result rather than an output parameter?

vtereshkov avatar Jul 22 '22 14:07 vtereshkov

sprintf requires you to allocate the string yourself and set the max cap.

          buf := str(make([]char, 256))
          sprintf(buf, "%d", i)
          ui.putButton(buf)

this bit of code can be condensed into

          ui.putButton(format("%d", i))

ske2004 avatar Jul 22 '22 19:07 ske2004

Couldn't the current sprintf just be reworked?

marekmaskarinec avatar Jul 22 '22 21:07 marekmaskarinec

i second that ^

ske2004 avatar Jul 27 '22 06:07 ske2004

@marekmaskarinec @skejeton My current idea is to change the sprintf signature from

fn sprintf(buf, format: str, a1: T1, a2: T2...): int

to

fn sprintf(format: str, a1: T1, a2: T2...): str

which is obviously incompatible with the existing code. Are you ready for this change?

vtereshkov avatar Aug 22 '22 23:08 vtereshkov