MaybeMonad 中的类型推断
Type inference in MaybeMonad
我正在从这个 site 中尝试一个示例,但是 运行 进入以下推理问题。
这个例子可能是关于 Monads 的,并且从连续的空检查中保存并以线性方式编写代码。
The type arguments for method Maybe<Customer>.Bind<TO>(Func<Customer,
Maybe<TO>>)
cannot be inferred from the usage. Try specifying the
type arguments explicitly.
Maybe.cs
using System;
namespace Monads
{
public class Maybe<T> where T : class
{
private readonly T value;
public Maybe(T someValue)
{
if(someValue == null)
throw new ArgumentNullException(nameof(someValue));
this.value = someValue;
}
private Maybe(){}
public Maybe<TO> Bind<TO>(Func<T, Maybe<TO>> func) where TO : class
{
return value != null ? func(value) : Maybe<TO>.None();
}
public static Maybe<T> None() => new Maybe<T>();
}
}
IMonadicRepository
namespace Monads
{
public interface IMonadicRepository
{
Maybe<Customer> GetCustomer(int id);
Maybe<Address> GetAddress(int id);
}
}
Repository.cs
namespace Monads {
public class Repository : IMonadicRepository
{
public Maybe<Customer> GetCustomer(int id) {
return new Maybe<Customer>(new Customer(id));
}
public Maybe<Address> GetAddress(int id)
{
return new Maybe<Address>(new Address(id));
}
}
}
Customer.cs
namespace Monads
{
public class Customer
{
public Customer(int id) {
}
public Address Address {get; set;}
}
}
Address.cs
namespace Monads
{
public class Address
{
public Address(int id) {
}
}
}
用法 - Programs.cs
namespace Monads
{
class Program
{
static void Main(string[] args)
{
IMonadicRepository repository = new Repository();
repository.GetCustomer(1)
.Bind(customer => customer.Address); // error
}
}
}
错误:无法从用法中推断方法 'Maybe.Bind(Func>)'
的类型参数。尝试明确指定类型参数。
你的问题是在 .Bind(customer => customer.Address)
行中,customer.Address
的类型是 Address
。然而,函数 Bind
需要一个 returns 和 Maybe<Address>
而不是 Address
.
的 lambda
我不确定这是否是正确的方法,但一种选择是使用 post:
中定义的 Return
扩展方法
repository.GetCustomer(1)
.Bind(c => c.Address.Return());
为了完成,我也复制在这里:
public static class MaybeExtensions
{
public static Maybe<T> Return<T>(this T value) where T : class
{
return value != null ? new Maybe<T>(value) : Maybe<T>.None();
}
}
我正在从这个 site 中尝试一个示例,但是 运行 进入以下推理问题。
这个例子可能是关于 Monads 的,并且从连续的空检查中保存并以线性方式编写代码。
The type arguments for method
Maybe<Customer>.Bind<TO>(Func<Customer, Maybe<TO>>)
cannot be inferred from the usage. Try specifying the type arguments explicitly.
Maybe.cs
using System;
namespace Monads
{
public class Maybe<T> where T : class
{
private readonly T value;
public Maybe(T someValue)
{
if(someValue == null)
throw new ArgumentNullException(nameof(someValue));
this.value = someValue;
}
private Maybe(){}
public Maybe<TO> Bind<TO>(Func<T, Maybe<TO>> func) where TO : class
{
return value != null ? func(value) : Maybe<TO>.None();
}
public static Maybe<T> None() => new Maybe<T>();
}
}
IMonadicRepository
namespace Monads
{
public interface IMonadicRepository
{
Maybe<Customer> GetCustomer(int id);
Maybe<Address> GetAddress(int id);
}
}
Repository.cs
namespace Monads {
public class Repository : IMonadicRepository
{
public Maybe<Customer> GetCustomer(int id) {
return new Maybe<Customer>(new Customer(id));
}
public Maybe<Address> GetAddress(int id)
{
return new Maybe<Address>(new Address(id));
}
}
}
Customer.cs
namespace Monads
{
public class Customer
{
public Customer(int id) {
}
public Address Address {get; set;}
}
}
Address.cs
namespace Monads
{
public class Address
{
public Address(int id) {
}
}
}
用法 - Programs.cs
namespace Monads
{
class Program
{
static void Main(string[] args)
{
IMonadicRepository repository = new Repository();
repository.GetCustomer(1)
.Bind(customer => customer.Address); // error
}
}
}
错误:无法从用法中推断方法 'Maybe.Bind(Func>)'
的类型参数。尝试明确指定类型参数。
你的问题是在 .Bind(customer => customer.Address)
行中,customer.Address
的类型是 Address
。然而,函数 Bind
需要一个 returns 和 Maybe<Address>
而不是 Address
.
我不确定这是否是正确的方法,但一种选择是使用 post:
中定义的Return
扩展方法
repository.GetCustomer(1)
.Bind(c => c.Address.Return());
为了完成,我也复制在这里:
public static class MaybeExtensions
{
public static Maybe<T> Return<T>(this T value) where T : class
{
return value != null ? new Maybe<T>(value) : Maybe<T>.None();
}
}