8

Consider the following code in AutoMapper

CreateMap<ViewModel, ServiceModel>()
    .ForMember(
        x => x.Type,
        opt => opt.MapFrom(y =>
            y.TypeName switch
            {
                "FirstName" => typeof(FirstModel),
                "SecondName" => typeof(SecondModel),
                _ => null
            }));

I have few different cases and I want to return a specific type for each string.

The compiler disagrees:

An expression tree may not contain a switch expression.

Why is that? I would expect that this feature, neatly called expression internally performs Expression.Switch, thus should be usable in expression trees.

I know that I can substitute with ternary operator syntax. I'd like to understand why it does not work.

Edit

Thanks Sweeper for pointing that that's not actually a switch expression. Guess I got carried on the convention train and got fooled by the naming.

Ivan Stoev I'm aware it's not supported but I am curious as to why. I understand why block scope is not supported, but switch case is very simple and declarative in nature, which would fit well in an expression tree.

Edit 2 Thanks to Jeroen Mostert comment bellow, I now understand that a switch case is far from simple internally and thus it has no place in expression trees.

11
  • 8
    Expression.Switch represents a switch statement, not a switch expression. Commented Dec 5, 2019 at 10:32
  • 4
    Because it's not implemented (similar to ?. operator: "An expression tree lambda may not contain a null propagating operator.", value tuples: "An expression tree may not contain a tuple literal." etc.) Commented Dec 5, 2019 at 10:51
  • 3
    A switch expression is anything but simple from the compiler's point of view. It may be compiled down to a sequence of if/thens, or a full-blown switch statement, it may involve tuple deconstruction, temporaries, constructor calls... It is not, in any case, considered a primitive expression of the expression tree API, which has traditionally allowed no newcomers in any case so as not to break providers (hence also no null propagation, no tuples, etc., even though those are conceptually much simpler than switch). Commented Dec 5, 2019 at 15:13
  • 11
    @EricLippert, you seem to have made a assumption about the nature of this question. No one was asking "why haven't the C# folks done this work." I think most here assumed there was a technical or philosophical reason for the exclusion of switch expressions. So why not inform us that this is up for grabs and provide a link to the repo and contribution guide? Personally, we make extensive use of Expression Tree. And I'm considering working on this. But I really hope this isn't representative of the attitudes I will encounter. Commented May 14, 2020 at 16:51
  • 2
    Moreover, the System.Linq.Expressions repo says it's not accepting contributions. Commented May 17, 2020 at 19:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.