Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active August 29, 2015 14:05
Show Gist options
  • Save Porges/3194c7f4655326579852 to your computer and use it in GitHub Desktop.
Save Porges/3194c7f4655326579852 to your computer and use it in GitHub Desktop.
public struct Result<T>(T value)
{
public static implicit operator Result<T>(T value) => new Result<T>(value);
public static implicit operator Result<T>(Result<Any> any) => default(Result<T>);
public static implicit operator Result<T>(Result<T, Any> value) => new Result<T>(value.Value);
public bool HasValue { get; } = true;
public T Value { get; } = value;
}
// Note that this doesn't even have readonly fields, which
// would be another 2 lines + uglier properties
public struct ResultOld<T>
{
public ResultOld(T value) : this()
{
HasValue = true;
Value = value;
}
public static implicit operator ResultOld<T>(T value)
{
return new ResultOld<T>(value);
}
public static implicit operator ResultOld<T>(Result<Any> any)
{
return default(ResultOld<T>);
}
public static implicit operator ResultOld<T>(Result<T, Any> value)
{
return new ResultOld<T>(value.Value);
}
public bool HasValue { get; private set; }
public T Value { get; private set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment