Skip to content

implicits: StringBuilder.clear()#401

Merged
mikesamuel merged 3 commits intomainfrom
stringbuilder-clear
Apr 7, 2026
Merged

implicits: StringBuilder.clear()#401
mikesamuel merged 3 commits intomainfrom
stringbuilder-clear

Conversation

@mikesamuel
Copy link
Copy Markdown
Contributor

Just adds a method to StringBuilder.

Just adds a method to StringBuilder.

Signed-off-by: Mike Samuel <mikesamuel@gmail.com>
Signed-off-by: Mike Samuel <mikesamuel@gmail.com>
}
val JavaLang.stringBuilderClear by receiver {
inlineSupport("StringBuilder::clear", arity = 1, needsSelf = true) { pos, args ->
args[0].method("setLength", J.IntegerLiteral(pos.rightEdge, 0), pos = pos)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strangely, Java's StringBuilder does not have a clear method but C# and Rust do.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strangely, Java's StringBuilder does not have a clear method but C# and Rust do.

Interesting. Not sure what I'd have guessed on that. (Since I forget a lot.)

stringBuilder ?: Js.Identifier(sbPos, JsIdentifierName("stringBuilder"), null),
Js.NumericLiteral(sbPos.rightEdge, 0),
computed = true,
voidExpr(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting void in front of an idiom is cheap and avoids leaking values.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting void in front of an idiom is cheap and avoids leaking values.

I'll try to remember.

Signed-off-by: Mike Samuel <mikesamuel@gmail.com>
}
val JavaLang.stringBuilderClear by receiver {
inlineSupport("StringBuilder::clear", arity = 1, needsSelf = true) { pos, args ->
args[0].method("setLength", J.IntegerLiteral(pos.rightEdge, 0), pos = pos)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strangely, Java's StringBuilder does not have a clear method but C# and Rust do.

Interesting. Not sure what I'd have guessed on that. (Since I forget a lot.)

stringBuilder ?: Js.Identifier(sbPos, JsIdentifierName("stringBuilder"), null),
Js.NumericLiteral(sbPos.rightEdge, 0),
computed = true,
voidExpr(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting void in front of an idiom is cheap and avoids leaking values.

I'll try to remember.

while #builder ~= 0 do
builder[#builder] = nil
end
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lua still often surprises me.

@mikesamuel mikesamuel merged commit 1fd4007 into main Apr 7, 2026
2 checks passed
@mikesamuel mikesamuel deleted the stringbuilder-clear branch April 7, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants