[runtime] Don't insta-fail when a faulty COM type is encountered. (#5616)
[mono.git] / mcs / tests / gtest-linq-20.cs
1 using System;
2
3 class Maybe<T>
4 {
5         public readonly static Maybe<T> Nothing = new Maybe<T> ();
6         public T Value { get; private set; }
7         public bool HasValue { get; private set; }
8         Maybe ()
9         {
10                 HasValue = false;
11         }
12         public Maybe (T value)
13         {
14                 Value = value;
15                 HasValue = true;
16         }
17
18         public override string ToString ()
19         {
20                 if (HasValue)
21                         return Value.ToString ();
22                 return string.Empty;
23         }
24
25         public Maybe<U> SelectMany<U> (Func<T, Maybe<U>> k)
26         {
27                 if (!HasValue)
28                         return Maybe<U>.Nothing;
29                 return k (Value);
30         }
31
32         public Maybe<V> SelectMany<U, V> (
33                 Func<T, Maybe<U>> selector,
34                 Func<T, U, V> resultSelector)
35         {
36                 if (!HasValue)
37                         return Maybe<V>.Nothing;
38                 Maybe<U> n = selector (Value);
39                 if (!n.HasValue)
40                         return Maybe<V>.Nothing;
41                 return resultSelector (Value, n.Value).ToMaybe ();
42         }
43 }
44
45 static class MaybeExtensions
46 {
47         public static Maybe<T> ToMaybe<T> (this T value)
48         {
49                 return new Maybe<T> (value);
50         }
51 }
52
53 class Test
54 {
55         public static void Main ()
56         {
57                 Console.WriteLine (
58                         from x in 1.ToMaybe ()
59                         from y in 2.ToMaybe ()
60                         from z in 3.ToMaybe ()
61                         select x + y + z);
62         }
63 }