我想了解更多關於函數式編程,使用language.ext(C#)庫作爲起點。我正在嘗試使用任一monad,但有一些我錯過了。見一個簡單的例子:開始與language.ext(功能C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dates
{
public class AssignmentDate
{
public static Either<string, AssignmentDate> Create(DateTime date)
{
if (date < MinValue)
return Left<string, AssignmentDate>("date out of range");
return Right<string, AssignmentDate>(date);
}
public static DateTime MinValue => new DateTime(1950, 1, 1);
}
}
左<類型>和右<>無法解析。我明顯錯過了一些東西,但是什麼?我正確使用<>嗎?這是使用Either <>時返回的正確方法嗎?任何人都可以指向language.ext的更多例子嗎?非常感謝您提供的任何幫助。
您需要導入包含的名稱空間,並使用'new Left <...>'和新的Right <...>'。 – Lee
感謝您回答李 - 通常Visual Studio是足夠好找到這一點,但通過LanguageExt.Core包看,我找不到Left <>和Right <>(我期望找到與Either <>相同的命名空間) 。這讓我覺得我誤解了一些根本性的東西 - 因此是一個問題。 –
我發現我錯過了 - 「使用靜態LanguageExt.Prelude;」這解決了事情正確。奇怪的是,Visual Studio的行爲與此不同(並且我無法通過對象瀏覽器找到任何東西)。謝謝你的幫助。 –