9

I'm trying to implement a "return if"/"return value if" without an else case, since I only want to return or return a value if a condition is valid.

I know, there is the if (condition) return; or if (condition) return value; but I want to have the code a bit cleaner AND it would be nice to have that syntax since it is more readable.

I heard with roslyn this is possible and read the question with its answeres here: Is there a way to implement custom language features in C#? but I have no clue how to implement it.

So the code would be something like this:

public class Testclass
{
    public static void Main(String[] args)
    {
        Testclass t = new Testclass();
        t.TestMyClassWithVoid();
        bool success = t.TestMyClassWithInt(3) == 3;
        bool fail = t.TestMyClassWithInt(2) == 2;
    }

    public void TestMyClassWithVoid()
    {
        int value = GetValueFromSomeWhere();
        return if value == 3;

        DoSomeOtherStuffSinceValueIsNotThree();
    }

    public int TestMyClassWithInt(int value)
    {
        return value if value == 3;

        DoSomeOtherStuffSinceValueIsNotThree();
        return -1;
    }
}

any idea how I could solve this? I started trying with the Roslyn.Compilers.CSharp-namespace but I have no clue how to start implementing.

Community
  • 1
  • 1
Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
  • what I understand from your code "return if value == 3;" that if the value is 3 you don't want to run the rest code of the method, right? – Ankit Aug 05 '16 at 10:04
  • 1
    @AnkitkumarBhatt yep! the same like `if(value == 3) return;` but the other way round – Matthias Burger Aug 05 '16 at 10:25
  • You can use goto with label, just put goto inside the if condition. Your purpose will be solved by this way I think. – Ankit Aug 05 '16 at 10:43
  • @AnkitkumarBhatt so I also could stay with the legal `if(value == 3) return;` syntax - more performance and much nicer than using a `goto` in C#. – Matthias Burger Aug 05 '16 at 10:51
  • Do you think return will work in case of void method? – Ankit Aug 05 '16 at 11:31
  • @AnkitkumarBhatt yes that would work – Matthias Burger Aug 05 '16 at 11:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120232/discussion-between-matthias-burger-and-ankitkumar-bhatt). – Matthias Burger Aug 05 '16 at 11:34
  • a `returnif` keyword is exactly what I'm looking for. what I'd like to write is `value == 3 returnif;` I don't want to have to wrap a long expression with an `if ()`, or extract it to a variable, but rather make a decision based on the currently evaluated expression. – Dave Cousineau Oct 09 '18 at 20:22

2 Answers2

1

If you can generate the correct IL for this, this should be work. Because this is not a new CLR capability you don't need do anything with dot net core, just Roslyn.

To do that you have two options,

One is to leave Roslyn as is and rewrite your new syntax to the correspond C# syntax and then compile as regular.

Second option, because Roslyn is an open source, is to add the ability to compile you new syntax to your own Roslyn and compile your code with it.

Dudi Keleti
  • 2,946
  • 18
  • 33
  • i took the second option and finally got it to work last weekend. thanks! – Matthias Burger Aug 08 '16 at 06:56
  • 2
    Could you expand on what you meant by "rewrite your new syntax to the corresponding C# syntax and compile as regular?" Do you mean doing so using Roslyn? I'm trying to find out if it's possible to use Roslyn to do so in a transparent way (i.e. it doesn't show up as some kind of IDE suggestion, but the transformation happens transparently at compile time?) – user1454265 Jan 11 '17 at 20:08
  • @user1454265 I mean that you can write what you want but before you compile you need to transform the code to a valid c# code. You can do it with Roslyn Code Fix or with separate process. But as fa as I know you can integrate it in the VS build process. – Dudi Keleti Jan 15 '17 at 10:11
  • I need optional refs in C# so we can move a giant million line VB.net project over to C#. Right now that is preventing us from moving, so the possibility of transparently rewriting optional refs to legal C# code in the background is VERY appealing. – Brain2000 Apr 29 '22 at 21:41
1

Here is an alternative suggestion that may work for someone who is looking for extension points into roslyn. It can even be provided as a nuget package! Create an analyser and a code fix provider for your syntax.

Essentially what you would get is a red squiggly line under any syntax of your choosing, with a auto-fix hint which would let you transform it into the regular C# equivalent.

It could also be used for validating the usage of your library, checking that the right attributes are in the right locations, detect common code smells, etc.

Read up more on code analysers at this post: https://msdn.microsoft.com/en-us/magazine/dn879356.aspx

A quick summary of that link (but by no means a replacement):

  • Install VS SDK, Roslyn SDK, Compiler Platform SDK
  • Create new project: Analyzer with Code Fix (NuGet + VSIX) template.
  • There will be 3 samples on how to create an analyser / code fix provider
  • Set the VSIX project as the startup project and run
caesay
  • 16,932
  • 15
  • 95
  • 160