Skip to content

Commit

Permalink
Add module designed to be statically imported
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Aug 22, 2019
1 parent 73065be commit f557ce3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 65 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ An auxiliary namespace is also provided:
using Optuple.Linq; // LINQ query syntax support
```


### Creating optional values

The most basic way to create optional values is to use the static `Option`
Expand Down Expand Up @@ -165,6 +166,25 @@ var stillSome = some.Filter(x => x == 10);
var none = some.Filter(x => x != 10);
```

### Helpers as global methods

If you [statically import][static-using] `OptionModule`:

```c#
using static Optuple.OptionModule;
```

it will make the following common methods available for use without type
qualification:

- `Some`
- `None<>`
- `SomeWhen`
- `NoneWhen`

This permits you to, for example, simply write `Some(42)` and `None<int>()`
instead of `Option.Some(42)` and `None<int>()`, respectively.

### Enumerating options

[comment]: # (Move somewhere?!)
Expand Down Expand Up @@ -218,3 +238,4 @@ project [Optional]. Thank you, [Nils L&uuml;ck][nlkl]!

[Optional]: https://www.nuget.org/packages/Optional/
[nlkl]: https://github.com/nlkl
[static-using]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static
12 changes: 12 additions & 0 deletions src/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ namespace Optuple
using System;
using System.Collections.Generic;

static partial class OptionModule
{
public static (bool HasValue, T Value) Some<T>(T value) => Option.Some(value);
public static (bool HasValue, T Value) None<T>() => Option.None<T>();

public static (bool HasValue, T Value) SomeWhen<T>(T value, Func<T, bool> predicate) =>
Option.SomeWhen(value, predicate);

public static (bool HasValue, T Value) NoneWhen<T>(T value, Func<T, bool> predicate) =>
Option.NoneWhen(value, predicate);
}

static partial class Option
{
public static (bool HasValue, T Value) Some<T>(T value) => (true, value);
Expand Down
1 change: 1 addition & 0 deletions src/Public.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Optuple
{
public partial class Option {}
public partial class OptionModule {}

namespace Linq
{
Expand Down
Loading

0 comments on commit f557ce3

Please sign in to comment.