question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add more string functions - StringBuilder extension

See original GitHub issue

Description

https://github.com/fsprojects/FSharpPlus/issues/158#issuecomment-512607693

For the StringBuilder extension, it will be used like below with the String.build function from my String.fs:

let s =
  String.build (fun builder ->
    builder.printfn "%s" "hello"
    builder.printfn "%i%i" 4 2
    ...
  )

The motivation to use this extension is to avoid the repetitive use of ignore caused by the fact StringBuilder’s Write and WriteLine returns the StringBuilder for method chaining. Also, sb.WriteLine(sprintf "%i" 42).WriteLine(sprintf "%s" "foo").ToString() looks odd and ugly imo, but this might be just a personal impression.

https://github.com/fsprojects/FSharpPlus/issues/158#issuecomment-512721780

the build and the StringBuilder functions looks nice to me, but let’s open another issue for StringBuilder extensions as at the moment we don’t have any of them, and it might require more discussion.

Referece implementation:

From cannorin/prelude.fs:


let build (builder: StringBuilder -> unit) =
  let sb = new StringBuilder()
  builder sb
  sb.ToString()

type StringBuilder with
  member inline this.printf format =
    Printf.kprintf (fun s -> this.Append s |> ignore) format

  member inline this.printfn format =
    Printf.kprintf (fun s -> this.AppendLine s |> ignore) format

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
gustycommented, Jul 31, 2019

I propose let’s put in hold for now, for the above reasons plus it looks to me like that proposal it’s gonna be approved. If it’s declined we can add something, either this original proposed code, or the builder from @vasily-kirichenko if he agrees.

0reactions
wallymathieucommented, Aug 1, 2019

Perhaps something to try out in a separate repository to see what’s possible?

Read more comments on GitHub >

github_iconTop Results From Across the Web

StringBuilder extension method for appending a collection ...
In C#, I'm trying to build an extension method for StringBuilder called AppendCollection() that would let me do this: var sb1 = new...
Read more >
StringBuilder - AppendIf - C# Extension Methods
A StringBuilder extension method that appends string values when condition is true.
Read more >
c# - An extension to the StringBuilder
The length property of a StringBuilder is read and writable. It's also really useful for it to be so: var sb = new...
Read more >
Extensions.Append(StringBuilder, StringSegment) Method
Add the given StringSegment to the StringBuilder.
Read more >
StringBuilder Extensions
These extension methods are adapted from those recently added to the LoycCore libraries. This set is more similar to the methods of System....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found