Skip to content

Instantly share code, notes, and snippets.

@freestylecoder
Last active May 20, 2016 14:01
Show Gist options
  • Save freestylecoder/50493ce7369f58e9555f0583dfa07077 to your computer and use it in GitHub Desktop.
Save freestylecoder/50493ce7369f58e9555f0583dfa07077 to your computer and use it in GitHub Desktop.
Passing F# With to C#
using System;
using Types;
namespace App {
class Program {
static void Main( string[] args ) {
Tag testTag = new Tag( 1, "Original" );
Console.WriteLine( "{0}: {1}", testTag.Id, testTag.Text );
Tag newTag = testTag.With( nameof( testTag.Id ), 2 ).With( nameof( testTag.Text ), "New One" );
Console.WriteLine( "{0}: {1}", newTag.Id, newTag.Text );
Console.ReadKey();
}
}
}
namespace Types
type Tag =
{
Id:int;
Text:string
}
member this.With( field:string, value:int ) =
match field with
| "Id" -> { this with Id = value }
| _ -> this
member this.With( field:string, value:string ) =
match field with
| "Text" -> { this with Text = value }
| _ -> this
@freestylecoder
Copy link
Author

Slightly safer (usage, not implementation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment