Announcement

Collapse
No announcement yet.

[CLOSED] [#3145] [16.3.1] Compiler error IndexOutOfRangeException with anonymous types

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    [CLOSED] [#3145] [16.3.1] Compiler error IndexOutOfRangeException with anonymous types

    I have some valid C# that results in an IndexOutOfRangeException from the Bridge compiler -

    public class Program
    {
        public static void Main()
        {
            var value = Optional<ResultOrError<string>>.Missing
                    .Match(
                    handleResult: items => new { Result = "abc" },
                    handleError: error => null,
                    handleNoValue: () => null
                );
        }
    }
    
    [Immutable]
    public struct Optional<T>
    {
        public Optional(T value) : this(value, value != null) { }
        private Optional(T value, bool isDefined)
        {
            IsDefined = isDefined && (value != null);
            Value = value;
        }
    
        public static Optional<T> Missing { get { return _missing; } }
        private static Optional<T> _missing = new Optional<T>(default(T), false);
    
        public bool IsDefined { get; private set; }
        public T Value { get; private set; }
    }
    
    public sealed class ResultOrError<T>
    {
        private readonly Optional<T> _result;
        private readonly Optional<string> _errorMessage;
        private ResultOrError(Optional<T> result, Optional<string> errorMessage)
        {
            _result = result;
            _errorMessage = errorMessage;
        }
    
        public TResult Match<TResult>(Func<T, TResult> handleResult, Func<string, TResult> handleError)
        {
            return _result.IsDefined ? handleResult(_result.Value) : handleError(_errorMessage.Value);
        }
    }
    
    public static class OptionalResultOrErrorExtensions
       {
        public static TResult Match<T, TResult>(this Optional<ResultOrError<T>> source, Func<TResult> handleNoValue, Func<T, TResult> handleResult, Func<string, TResult> handleError)
        {
            if (!source.IsDefined)
                return handleNoValue();
    
            return source.Value.Match(handleResult, handleError);
        }      
    }
    (Also available at https://deck.net/dc3030fcf2c7bea8ca40efc825316c50)

    #2
    Fix for issue #3145 merged into v16.3.1 branch and will be included in the next Bridge.NET release.

    Comment

    Working...
    X