Last active
May 20, 2016 14:01
-
-
Save freestylecoder/50493ce7369f58e9555f0583dfa07077 to your computer and use it in GitHub Desktop.
Passing F# With to C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slightly safer (usage, not implementation)