Why don`t be functional? I mean immutability

let sb = sb.Append('F')

this way you will clearly show that sb now have new meaning regardless of internal implementation of StringBuiledr

By on 9/24/2009 7:08 AM ()

Hi,

I don't like this last solution. When a function returns unit (or when there's a call to "ignore"), you make it clear there is a side-effect. If you use a useless let, people might think there's no side-effect, this can be surprising and dangerous.

What happens if, later, someone changes the code into: "let other = sb.Append('F')", thinking he'll be able to access the old value? I don't like the idea of hiding this kind of side-effect.

Laurent.

By on 9/24/2009 11:25 AM ()

You can always add compiler directive in script to suppress some warnings, as in this case =>

#nowarn "20"

By on 9/22/2009 4:48 AM ()

Hi,

I recommend you to use extension methods, e.g.

1
2
type System.Text.StringBuilder with
  member sb.AppendI (s: string) = sb.Append s |> ignore

Laurent.

By on 6/24/2008 4:18 PM ()

Thanks Laurent!

Based on what you provided, this met my needs:

type System.Text.StringBuilder with
member sb.String(s: string) = sb.Append s |> ignore
member sb.Char(c: char) = sb.Append c |> ignore
member sb.NewLine() = sb.Char('\n')

Cameron

By on 6/24/2008 5:30 PM ()

I was hoping to add another member named "sb.Sprintf", but my functional programming skills are failing me. Anyone know how? Ideally, I would like to do:

let name = "Cameron"
sb.Sprintf "Name: %s" name

cheers,
Cameron

By on 7/16/2008 2:48 PM ()

Also, have a look at the Microsoft.FSharp.Text.Printf module. There are some useful functions that take a StringBuilder like:

1
2
3
4
val bprintf : StringBuilder -> #BuilderFormat<'a> -> 'a


val kbprintf : (unit -> 'd) -> StringBuilder -> #BuilderFormat<'a,'d> -> 'a
By on 7/18/2008 8:00 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper