Search Unity

What's wrong with my Bool Awaiter?

Discussion in 'Scripting' started by bitinn, Apr 4, 2019.

  1. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    So I was trying to make a simple awaitable struct that allow my to return a bool:

    Code (CSharp):
    1.     // define a special container for returning results
    2.     public struct BoolResult {
    3.         public bool Value;
    4.         public BoolAwaiter GetAwaiter () {
    5.             return new BoolAwaiter(Value);
    6.         }
    7.     }
    8.  
    9.     // make the interface task-like
    10.     public readonly struct BoolAwaiter : INotifyCompletion {
    11.         private readonly bool _Input;
    12.  
    13.         // wrap the async operation
    14.         public BoolAwaiter (bool value) {
    15.             _Input = value;
    16.         }
    17.  
    18.         // is task already done (yes)
    19.         public bool IsCompleted {
    20.             get { return true; }
    21.         }
    22.  
    23.         // wait until task is done (never called)
    24.         public void OnCompleted (Action continuation) => continuation?.Invoke();
    25.  
    26.         // return the result
    27.         public bool GetResult () {
    28.             return _Input;
    29.         }
    30.     }
    And I was using it like:

    Code (CSharp):
    1. private async BoolResult LoadAssets (string label) { ... return new BoolResult { Value = true }; }
    I don't get why C# tells me:

    Didn't my BoolResult already meet the "Task-like" requirement?
     
  2. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    So people told me I need to declare AsyncMethodBuilder, because Task-like only makes it await-able.

    But to return this thing in an async function, is a different matter.

    https://github.com/dotnet/roslyn/blob/master/docs/features/task-types.md

    Problem is, I need AsyncMethodBuilder attribute, and I don't think it's available in Unity 2018.3?

    With
    Code (CSharp):
    1. using System.Runtime.CompilerServices;
    AsyncMethodBuilder is still not found... (I think Unity 2018.3 has C# 7 support, but perhaps it was introduced at later version?)
     
    Last edited: Apr 4, 2019
  3. bitinn

    bitinn

    Joined:
    Aug 20, 2016
    Posts:
    961
    I am now starting to understand why all those Async library for Unity ships their own CompileServices.dll...