-
Notifications
You must be signed in to change notification settings - Fork 146
/
Person.cs
45 lines (36 loc) · 1.36 KB
/
Person.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using Microsoft.FSharp.Core;
using Errors = FSharpx.Collections.NonEmptyList<string>;
namespace FSharpx.CSharpTests {
public class Person {
private readonly string name;
private readonly int age;
public string Name {
get { return name; }
}
public int Age {
get { return age; }
}
private Person(string name, int age) {
this.name = name;
this.age = age;
}
private static readonly Func<string, int, Person> New = (name, age) => new Person(name, age);
public static FSharpChoice<Person, Errors> TryNew(string name, int age) {
return New.Curry().ReturnValidation()
.ApValidation(Mandatory(name))
.ApValidation(Positive(age));
}
private static FSharpChoice<string, Errors> Mandatory(string s) {
return FSharpChoice.Validator<string>(x => !string.IsNullOrEmpty(x), "Mandatory field")(s);
//if (string.IsNullOrEmpty(s))
// return FSharpChoice.Error<string>("Mandatory field");
//return FSharpChoice.Ok(s);
}
private static FSharpChoice<int, Errors> Positive(int a) {
if (a <= 0)
return FSharpChoice.Error<int>("Field must be positive");
return FSharpChoice.Ok(a);
}
}
}