├── .gitignore ├── LICENSE ├── MaybeAsAStruct ├── ExtensionMethods.cs ├── Maybe.cs ├── MaybeAsAStruct.csproj └── Program.cs ├── MaybeAsASumType ├── Maybe.cs ├── MaybeAsASumType.csproj └── Program.cs ├── MaybeExamples.sln └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Yacoub Massad 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MaybeAsAStruct/ExtensionMethods.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace MaybeAsAStruct 6 | { 7 | public static class ExtensionMethods 8 | { 9 | public static Maybe FirstOrNone(this IEnumerable enumerable, Func predicate) 10 | { 11 | return enumerable.Where(predicate).FirstOrNone(); 12 | } 13 | 14 | public static Maybe FirstOrNone(this IEnumerable enumerable) 15 | { 16 | if (enumerable == null) 17 | throw new ArgumentNullException(nameof(enumerable)); 18 | 19 | if (enumerable is IList list) 20 | { 21 | if (list.Count > 0) return list[0]; 22 | } 23 | else 24 | { 25 | using (IEnumerator e = enumerable.GetEnumerator()) 26 | { 27 | if (e.MoveNext()) return e.Current; 28 | } 29 | } 30 | 31 | return new Maybe(); 32 | } 33 | 34 | public static Maybe TryParseToInt(this string str) 35 | { 36 | if (int.TryParse(str, out var result)) 37 | return result; 38 | 39 | return Maybe.None; 40 | } 41 | 42 | public static T[] ValueOrEmptyArray(this Maybe maybe) 43 | { 44 | return maybe.ValueOr(Array.Empty()); 45 | } 46 | 47 | public static string ValueOrEmptyString(this Maybe maybe) 48 | { 49 | return maybe.ValueOr(string.Empty); 50 | } 51 | 52 | public static IEnumerable GetItemsWithValue(this IEnumerable> enumerable) 53 | { 54 | foreach (var maybe in enumerable) 55 | { 56 | if (maybe.TryGetValue(out var value)) 57 | yield return value; 58 | } 59 | } 60 | 61 | public static Maybe> IfAllHaveValues(this IEnumerable> enumerable) 62 | { 63 | var result = new List(); 64 | 65 | foreach (var maybe in enumerable) 66 | { 67 | if (!maybe.TryGetValue(out var value)) 68 | return Maybe.None; 69 | 70 | result.Add(value); 71 | } 72 | 73 | return result; 74 | } 75 | 76 | public static AddIfHasValue ToAddIfHasValue(this Maybe maybe) 77 | { 78 | return new AddIfHasValue(maybe); 79 | } 80 | 81 | public static void Add(this ICollection collection, AddIfHasValue addIfHasValue) 82 | { 83 | if (addIfHasValue.Maybe.TryGetValue(out var value)) 84 | { 85 | collection.Add(value); 86 | } 87 | } 88 | } 89 | 90 | public struct AddIfHasValue 91 | { 92 | public Maybe Maybe { get; } 93 | 94 | public AddIfHasValue(Maybe maybe) 95 | { 96 | Maybe = maybe; 97 | } 98 | } 99 | } -------------------------------------------------------------------------------- /MaybeAsAStruct/Maybe.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MaybeAsAStruct 4 | { 5 | public struct Maybe 6 | { 7 | private readonly T value; 8 | 9 | private readonly bool hasValue; 10 | 11 | private Maybe(T value) 12 | { 13 | this.value = value; 14 | hasValue = true; 15 | } 16 | 17 | public TResult Match(Func some, Func none) 18 | { 19 | if (hasValue) 20 | return some(value); 21 | 22 | return none(); 23 | } 24 | 25 | public void Match(Action some, Action none) 26 | { 27 | if (hasValue) 28 | { 29 | some(value); 30 | } 31 | else 32 | { 33 | none(); 34 | } 35 | } 36 | 37 | public static implicit operator Maybe(T value) 38 | { 39 | if(value == null) 40 | return new Maybe(); 41 | 42 | return new Maybe(value); 43 | } 44 | 45 | public static implicit operator Maybe(Maybe.MaybeNone value) 46 | { 47 | return new Maybe(); 48 | } 49 | 50 | public bool TryGetValue(out T value) 51 | { 52 | if (hasValue) 53 | { 54 | value = this.value; 55 | return true; 56 | } 57 | 58 | value = default(T); 59 | return false; 60 | } 61 | 62 | public Maybe Map(Func convert) 63 | { 64 | if(!hasValue) 65 | return new Maybe(); 66 | 67 | return convert(value); 68 | } 69 | 70 | public Maybe Select(Func convert) 71 | { 72 | if (!hasValue) 73 | return new Maybe(); 74 | 75 | return convert(value); 76 | } 77 | 78 | public Maybe Bind(Func> convert) 79 | { 80 | if (!hasValue) 81 | return new Maybe(); 82 | 83 | return convert(value); 84 | } 85 | 86 | public Maybe SelectMany( 87 | Func> convert, 88 | Func finalSelect) 89 | { 90 | if (!hasValue) 91 | return new Maybe(); 92 | 93 | var converted = convert(value); 94 | 95 | if (!converted.hasValue) 96 | return new Maybe(); 97 | 98 | return finalSelect(value, converted.value); 99 | } 100 | 101 | public Maybe Where(Func predicate) 102 | { 103 | if (!hasValue) 104 | return new Maybe(); 105 | 106 | if (predicate(value)) 107 | return this; 108 | 109 | return new Maybe(); 110 | } 111 | 112 | public T ValueOr(T defaultValue) 113 | { 114 | if (hasValue) 115 | return value; 116 | 117 | return defaultValue; 118 | } 119 | 120 | public T ValueOr(Func defaultValueFactory) 121 | { 122 | if (hasValue) 123 | return value; 124 | 125 | return defaultValueFactory(); 126 | } 127 | 128 | public Maybe ValueOrMaybe(Maybe alternativeValue) 129 | { 130 | if (hasValue) 131 | return this; 132 | 133 | return alternativeValue; 134 | } 135 | 136 | public Maybe ValueOrMaybe(Func> alternativeValueFactory) 137 | { 138 | if (hasValue) 139 | return this; 140 | 141 | return alternativeValueFactory(); 142 | } 143 | 144 | public T ValueOrThrow(string errorMessage) 145 | { 146 | if (hasValue) 147 | return value; 148 | 149 | throw new Exception(errorMessage); 150 | } 151 | } 152 | 153 | public static class Maybe 154 | { 155 | public class MaybeNone 156 | { 157 | } 158 | 159 | public static MaybeNone None { get; } = new MaybeNone(); 160 | 161 | public static Maybe Some(T value) 162 | { 163 | if (value == null) 164 | throw new ArgumentNullException(nameof(value)); 165 | 166 | return value; 167 | } 168 | } 169 | } -------------------------------------------------------------------------------- /MaybeAsAStruct/MaybeAsAStruct.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MaybeAsAStruct/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | namespace MaybeAsAStruct 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | 13 | } 14 | 15 | static void Test2() 16 | { 17 | var contents = GetLogContents(1); 18 | 19 | if (contents.TryGetValue(out var value)) 20 | { 21 | Console.WriteLine(value); 22 | } 23 | else 24 | { 25 | Console.WriteLine("Log file not found"); 26 | } 27 | } 28 | 29 | static void Test3() 30 | { 31 | var contents = GetLogContents(1); 32 | 33 | contents.Match(some: value => 34 | { 35 | Console.WriteLine(value); 36 | }, 37 | none: () => 38 | { 39 | Console.WriteLine("Log file not found"); 40 | }); 41 | } 42 | 43 | static void Test4() 44 | { 45 | var errorDescriptionMaybe = 46 | GetLogContents(13) 47 | .Bind(contents => FindErrorCode(contents)) 48 | .Bind(errorCode => GetErrorDescription(errorCode)); 49 | } 50 | 51 | static void Test5() 52 | { 53 | var errorDescriptionMaybe = 54 | GetLogContents(13) 55 | .Bind(contents => FindErrorCode(contents) 56 | .Bind(errorCode => GetErrorDescription(errorCode, contents))); 57 | } 58 | 59 | static void Test6() 60 | { 61 | var errorDescriptionMaybe = 62 | from contents in GetLogContents(13) 63 | from errorCode in FindErrorCode(contents) 64 | from errorDescription in GetErrorDescription(errorCode, contents) 65 | select errorDescription; 66 | } 67 | 68 | static void Test7() 69 | { 70 | var errorDescriptionMaybe = 71 | from contents in GetLogContents(13) 72 | from errorCode in FindErrorCode(contents) 73 | where errorCode < 1000 74 | from errorDescription in GetErrorDescription(errorCode, contents) 75 | select errorDescription; 76 | } 77 | 78 | static void Test8() 79 | { 80 | var errorMessage = 81 | GetErrorDescription(15) 82 | .ValueOr("Unknown error"); 83 | } 84 | 85 | static void Test9() 86 | { 87 | var errorMessage = 88 | GetErrorDescription(15) 89 | .ValueOr(() => GetDefaultErrorMessage()); 90 | } 91 | 92 | static void Test10() 93 | { 94 | var errorMessage = 95 | GetErrorDescription(15) 96 | .ValueOrMaybe(GetErrorDescriptionViaWebService(15)) 97 | .ValueOr("Unknown error"); 98 | } 99 | 100 | static void Test11() 101 | { 102 | var errorMessage = 103 | GetErrorDescription(15) 104 | .ValueOrMaybe(() => GetErrorDescriptionViaWebService(15)) 105 | .ValueOr("Unknown error"); 106 | } 107 | 108 | static void Test12() 109 | { 110 | var logContents = 111 | GetLogContents(1) 112 | .ValueOrThrow("Unable to get log contents"); 113 | } 114 | 115 | static void Test14() 116 | { 117 | List multipleLogContents = 118 | Enumerable.Range(1, 20) 119 | .Select(x => GetLogContents(x)) 120 | .GetItemsWithValue() 121 | .ToList(); 122 | } 123 | 124 | static void Test15() 125 | { 126 | List multipleLogContents = 127 | Enumerable.Range(1, 20) 128 | .Select(x => GetLogContents(x)) 129 | .IfAllHaveValues() 130 | .ValueOrThrow("Some logs are not available") 131 | .ToList(); 132 | } 133 | 134 | static void Test16() 135 | { 136 | Maybe logMaybe = Maybe.Some("entry9"); 137 | 138 | var list = new List() 139 | { 140 | "entry1", 141 | logMaybe.ToAddIfHasValue(), 142 | "entry2" 143 | }; 144 | } 145 | 146 | static string GetDefaultErrorMessage() 147 | { 148 | return File.ReadAllText("c:\\defaultErrorMessage.txt"); 149 | } 150 | 151 | static Maybe GetLogContents(int id) 152 | { 153 | var filename = "c:\\logs\\" + id + ".log"; 154 | 155 | if (File.Exists(filename)) 156 | return File.ReadAllText(filename); 157 | 158 | return Maybe.None; 159 | } 160 | 161 | static Maybe FindErrorCode(string logContents) 162 | { 163 | var logLines = logContents.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); 164 | 165 | return 166 | logLines 167 | .FirstOrNone(x => x.StartsWith("Error code: ")) 168 | .Map(x => x.Substring("Error code: ".Length)) 169 | .Bind(x => x.TryParseToInt()); 170 | } 171 | 172 | static Maybe GetErrorDescription(int errorCode) 173 | { 174 | var filename = "c:\\errorCodes\\" + errorCode + ".txt"; 175 | 176 | if (File.Exists(filename)) 177 | return File.ReadAllText(filename); 178 | 179 | return Maybe.None; 180 | } 181 | 182 | static Maybe GetErrorDescription(int errorCode, string logContents) 183 | { 184 | var logLines = logContents.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); 185 | 186 | var linePrefix = "Error description for code " + errorCode + ": "; 187 | 188 | return 189 | logLines 190 | .FirstOrNone(x => x.StartsWith(linePrefix)) 191 | .Map(x => x.Substring(linePrefix.Length)); 192 | } 193 | 194 | static Maybe GetErrorDescriptionViaWebService(int errorCode) 195 | { 196 | //Real code would call a web service 197 | return Maybe.None; 198 | } 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /MaybeAsASumType/Maybe.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace MaybeAsASumType 6 | { 7 | public abstract class Maybe 8 | { 9 | private Maybe() 10 | { 11 | } 12 | 13 | public sealed class Some : Maybe 14 | { 15 | public Some(T value) => Value = value; 16 | public T Value { get; } 17 | } 18 | 19 | public sealed class None : Maybe 20 | { 21 | } 22 | 23 | public TResult Match(Func some, Func none) 24 | { 25 | if (this is Some s) 26 | return some(s.Value); 27 | 28 | return none(); 29 | } 30 | 31 | public void Match(Action some, Action none) 32 | { 33 | if (this is Some s) 34 | { 35 | some(s.Value); 36 | } 37 | else 38 | { 39 | none(); 40 | } 41 | } 42 | 43 | public static implicit operator Maybe(T value) 44 | { 45 | if (value == null) 46 | return new None(); 47 | 48 | return new Some(value); 49 | } 50 | 51 | public static implicit operator Maybe(Maybe.MaybeNone value) 52 | { 53 | return new None(); 54 | } 55 | 56 | public bool TryGetValue(out T value) 57 | { 58 | if (this is Some some) 59 | { 60 | value = some.Value; 61 | return true; 62 | } 63 | 64 | value = default(T); 65 | return false; 66 | } 67 | } 68 | 69 | public static class Maybe 70 | { 71 | public class MaybeNone 72 | { 73 | } 74 | 75 | public static MaybeNone None { get; } = new MaybeNone(); 76 | 77 | public static Maybe Some(T value) 78 | { 79 | if (value == null) 80 | throw new ArgumentNullException(nameof(value)); 81 | 82 | return new Maybe.Some(value); 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /MaybeAsASumType/MaybeAsASumType.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MaybeAsASumType/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | namespace MaybeAsASumType 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | 11 | } 12 | 13 | static void Test1() 14 | { 15 | var contents = GetLogContents(1); 16 | 17 | if (contents is Maybe.Some some) 18 | { 19 | Console.WriteLine(some.Value); 20 | } 21 | else 22 | { 23 | Console.WriteLine("Log file not found"); 24 | } 25 | } 26 | 27 | static void Test2() 28 | { 29 | var contents = GetLogContents(1); 30 | 31 | if (contents.TryGetValue(out var value)) 32 | { 33 | Console.WriteLine(value); 34 | } 35 | else 36 | { 37 | Console.WriteLine("Log file not found"); 38 | } 39 | } 40 | 41 | static void Test3() 42 | { 43 | var contents = GetLogContents(1); 44 | 45 | contents.Match(some: value => 46 | { 47 | Console.WriteLine(value); 48 | }, 49 | none: () => 50 | { 51 | Console.WriteLine("Log file not found"); 52 | }); 53 | } 54 | 55 | static Maybe GetLogContents(int id) 56 | { 57 | var filename = "c:\\logs\\" + id + ".log"; 58 | 59 | if (File.Exists(filename)) 60 | return File.ReadAllText(filename); 61 | 62 | return Maybe.None; 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /MaybeExamples.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.489 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaybeAsASumType", "MaybeAsASumType\MaybeAsASumType.csproj", "{C175D6CA-4B0F-44A6-9258-29B96774E8C1}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaybeAsAStruct", "MaybeAsAStruct\MaybeAsAStruct.csproj", "{98158549-5996-4063-BB1F-4488C575D7F0}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {C175D6CA-4B0F-44A6-9258-29B96774E8C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {C175D6CA-4B0F-44A6-9258-29B96774E8C1}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {C175D6CA-4B0F-44A6-9258-29B96774E8C1}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {C175D6CA-4B0F-44A6-9258-29B96774E8C1}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {98158549-5996-4063-BB1F-4488C575D7F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {98158549-5996-4063-BB1F-4488C575D7F0}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {98158549-5996-4063-BB1F-4488C575D7F0}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {98158549-5996-4063-BB1F-4488C575D7F0}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {B058F875-DCC8-4EA8-8343-E6BE6DA1B375} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MaybeExamples --------------------------------------------------------------------------------