-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/monad-completion-source' into 'develop'
Monad completion source See merge request engine/mast!9
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections; | ||
|
||
namespace Rayark.Mast | ||
{ | ||
/// <summary> | ||
/// Represents the producer side of a <see cref="IMonad{T}"/> unbound to a coroutine, | ||
/// providing access to the consumer side through the Monad property. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the result value associated with this <see cref="MonadCompletionSource{T}"/></typeparam> | ||
public class MonadCompletionSource<T> : IReturn<T> | ||
{ | ||
|
||
#region private types | ||
private class InternalMonad : IMonad<T>, IEnumerator | ||
{ | ||
public bool IsDone = false; | ||
|
||
public T Result | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
public Exception Error | ||
{ | ||
get; | ||
set; | ||
} | ||
public IEnumerator Do() | ||
{ | ||
return this; | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
return !IsDone; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
} | ||
|
||
public object Current | ||
{ | ||
get { return null; } | ||
} | ||
} | ||
#endregion | ||
|
||
|
||
readonly InternalMonad _monad = new InternalMonad(); | ||
|
||
public IMonad<T> Monad | ||
{ | ||
get { return _monad; } | ||
} | ||
|
||
public void Accept(T result) | ||
{ | ||
if (_monad.IsDone) | ||
throw new System.Exception("Already done"); | ||
_monad.Result = result; | ||
_monad.IsDone = true; | ||
} | ||
|
||
public void Fail(Exception error) | ||
{ | ||
if (_monad.IsDone) | ||
throw new System.Exception("Already done"); | ||
_monad.Error = error; | ||
_monad.IsDone = true; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.