Last active
September 4, 2015 14:16
-
-
Save mjul/5c345714b1f92b21fd94 to your computer and use it in GitHub Desktop.
Polyglot F# / C# mixture using F# to easily define Value Types (in the DDD sense - they are realized as .NET classes). Shows using types and discriminated unions from 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
namespace Functionglot | |
type Customer = { First : string; Last: string; SSN: uint32; AccountNumber : uint32; } | |
type Shape = | |
| Rectangle of width : float * length : float | |
| Circle of radius : float | |
| EquilateralTriangle of side : float | |
| Square of side : float | |
module Calculations = | |
let area myShape = | |
match myShape with | |
| Circle radius -> System.Math.PI * radius * radius | |
| EquilateralTriangle s -> (sqrt 3.0) / 4.0 * s * s | |
| Square s -> s * s | |
| Rectangle (h, w) -> h * w |
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 System.IO; | |
using Functionglot; | |
using NUnit.Framework; | |
namespace Polyglot | |
{ | |
[TestFixture] | |
public class UnitTest1 | |
{ | |
[Test] | |
public void Customer_FSharpType_Test() | |
{ | |
var customer = new Functionglot.Customer("Martin", "von Eftername", 123, 999); | |
Assert.AreEqual("Martin", customer.First); | |
} | |
[Test] | |
public void Shape_FSharpDiscriminatedUnion_Area_Circle() | |
{ | |
Functionglot.Shape circle = Functionglot.Shape.NewCircle(1.0); | |
Assert.AreEqual(Math.PI, Functionglot.Calculations.area(circle)); | |
} | |
[Test] | |
public void Shape_FSharpDiscriminatedUnion_Area_Square() | |
{ | |
Functionglot.Shape square = Functionglot.Shape.NewSquare(2.0); | |
Assert.AreEqual(4.0, Functionglot.Calculations.area(square)); | |
} | |
[Test] | |
public void Shape_FSharpDiscriminatedUnion_TypeTasting() | |
{ | |
Functionglot.Shape shape = Functionglot.Shape.NewSquare(2.0); | |
// There are IsXxxxx properties for each of the types in the discriminated union | |
Assert.That(shape.IsSquare); | |
} | |
[Test] | |
public void FSharpDiscriminatedUnion_InstancesAreSerializable() | |
{ | |
var rectangle = Functionglot.Shape.NewRectangle(1.0, 2.0); | |
var deserialized = SerializeDeserialize(rectangle); | |
Assert.That(deserialized, Is.EqualTo(rectangle)); | |
} | |
[Test] | |
public void FSharpType_InstancesAreSerializable() | |
{ | |
var customer = new Functionglot.Customer("Martin", "von Eftername", 123, 999); | |
var deserialized = SerializeDeserialize(customer); | |
Assert.That(deserialized, Is.EqualTo(customer)); | |
} | |
private static T SerializeDeserialize<T>(T rectangle) | |
{ | |
var ms = new MemoryStream(); | |
var serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); | |
serializer.Serialize(ms, rectangle); | |
ms.Position = 0; | |
var deserialized = (T)serializer.Deserialize(ms); | |
return deserialized; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment