Hi,
I am trying to build higher abstraction based on smaller Try function components but resulting "combination" is eager in result instead of being lazy:
namespace LanguageExtTesting
{
class Program
{
static Try<int> Parse(string str) => () => int.Parse(str);
static Try<int> Sum(string lhs, string rhs)
=> from x in Parse(rhs)
from y in Parse(lhs)
select x + y;
static void Main(string[] args)
{
var trySum = Sum("1r", "2"); // 1. throws here
var i = trySum.Try(); // 2. instead of here
}
}
}
problem here is that Program currently throws in the line 1. instead of line 2. . I tried wrapping LING expression within Sum() function with additional "() =>" but then I am getting compilation error of Result
This passes for me on master, so no exception is thrown anywhere.
using Xunit;
namespace LanguageExt.Tests
{
public class Tests
{
static Try<int> Parse(string str) => () => int.Parse(str);
static Try<int> Sum(string lhs, string rhs)
=> from x in Parse(rhs)
from y in Parse(lhs)
select x + y;
[Fact]
public void Test()
{
var trySum = Sum("1r", "2");
var i = trySum.Try();
}
}
}
What version are you using?
Latest NuGet version. Not that familiar with xunit, what is the assertion here?
what is the assertion here?
The implicit assertion is that no exception is thrown.
Hm shouldn't then the line "var i = trySum.Try();" throw?
No, it is in the faulted state. I get the same behavior for version 3.3.28.
using LanguageExt.Common;
using Xunit;
namespace LanguageExt.Tests
{
public class Tests
{
static Try<int> Parse(string str) => () => int.Parse(str);
static Try<int> Sum(string lhs, string rhs)
=> from x in Parse(rhs)
from y in Parse(lhs)
select x + y;
[Fact]
public void Test()
{
var trySum = Sum("1r", "2");
Result<int> actual = trySum.Try();
Assert.True(actual.IsFaulted);
}
}
}
@slimshader Don't use Try<A>.Try() directly, use the other extension methods of Try<A> to work with the value. So, Try<A>.Match(...), Try<A>.IfFail(...), etc.
Sorry for late reply, thanks a lot for your answers, turns out it was me being stupid and overinterpreting Visual Studio's exception notification. So to clarify: the LINQ expression I used to build Sum() function is the right way to go about combining Try<> (similarly to other monads)?
Yes. It is one is the correct ways.