0

This is a follow-up to a question I asked earlier, but is (I think) much simpler, so I'm asking it separately. To clarify the question, I've stripped this one down even further.

The code here uses the Language-Ext Nuget package.

The only data structure needed here is the following...

record Trans(int Id, decimal Amount)

I have a (very much simplified) method that calls an external service to approve a transaction...

static async Task<Either<int, string>> CallService(Trans transaction) =>
  transaction.Amount < 1.5M
    ? "Success"
    : -1;

I want to call this as part of code like this (again, very much simplified)...

static async Task<Either<int, string>> ApproveRefund(Trans transaction) =>
  await (
    from result in CallService(transaction).ToAsync()
    select result
  )
  .Match(result => result, s => s);

However, I get a compiler error "Error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement" on the two lambdas I pass to the Match method.

I can't work out what I've done wrong here. Anyone able to help? Thanks

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • What does that LINQ query do? A [mcve] would make it much easier to help you. My *guess* is that the precedence of `await` vs `.` is confusing things, but without more details it's hard to know. – Jon Skeet May 08 '23 at 17:44
  • @JonSkeet That is actually an MRE, I copied it directly out of LinqPad. The Linq query is a very short version of one that gathers various bits of data, optionally calls the external service (mocked here by `CallService`) and then saves the data (not shown). A fuller explanation is found in the other question. I didn't repeat it all here as I didn't think it relevant. My guess is that this is a syntax issue, but I'm not sure what. – Avrohom Yisroel May 08 '23 at 18:47
  • @JonSkeet Also, please can you clarify what you meant about the precedence of `await` vs `.` as I'm not clear. I've done queries like this many times before and not had this issue. Thanks – Avrohom Yisroel May 08 '23 at 18:48
  • No, that's definitely *not* an MRE. Where is `Either` declared, for example? What about `Match`? Note that while LinqPad is a great tool, it's not a good idea to assume that everyone who might answer your question will have access to it. If you can provide a console app which folks can copy/paste/compile/run, you'll make it easier for far more people to help you. (I'd rather not go into the details of my speculation around precedence until I've actually got something I can check.) – Jon Skeet May 08 '23 at 18:48
  • @JonSkeet Sorry, I assumed that the presence of the `language-ext` tag, and its mention in the title made it clear enough that I was using that Package. I'll update the question to make this clear. – Avrohom Yisroel May 08 '23 at 18:52

1 Answers1

1

I think you'd need to be more explicit with the return types...

.Match(Right<int, string>, Left<int, string>);

I'm not actually sure why you need to do this, but I have hit this issue occasionally, and this fixed it.

Maybe one of the experts can explain why, but that should get you going.

DreamingOfSleep
  • 1,208
  • 1
  • 11
  • 23