vnorilo 5 days ago

The recent C# feature called interceptors [1] pretty much looks like comefrom from where I stand. Yet everyone talking about it has either been serious, or very good at trolling.

1: https://khalidabuhakmeh.com/dotnet-8-interceptors

2
skissane 5 days ago

From your link:

[InterceptsLocation("/Users/khalidabuhakmeh/RiderProjects/ConsoleApp12/ConsoleApp12/Program.cs", line: 3, character: 3)]

they added a language feature which is sensitive to precise line/character offsets in your source code, so the tiniest change to the source code invalidates your code…

I’m speechless. Whatever they are aiming to achieve here, surely there is a more elegant, less ugly way

poizan42 5 days ago

You are not supposed to use interceptors in code you write yourself. The feature exists for Roslyn Source Generators that runs every time you build the code.

ninkendo 4 days ago

I’m still confused though, if you’re generating the code anyway, why do you need an interceptor? Can’t you just generate the code to match what you want to redirect to, directly inline?

poizan42 4 days ago

Yes if all the code was generated. The problem is when you want to modify the behavior of user-supplied code - Roslyn Source Generators are additive so you cannot make modifications directly to user-supplied code.

You can read about how they work here: https://github.com/dotnet/roslyn/blob/main/docs/features/inc...

Basically they get the files (and ambient metadata) that are part of the compilation, filter to the parts it depends on, transforms to a in-mem representation of the data needed for the code generation, and then finally adds new files to the compilation. Since they can only add new files they cannot e.g. add code to be executed before or after user code is executed like with AOP. Interceptors are a solution to that problem.

ninkendo 4 days ago

Interesting, so you’re saying generated code can change the behavior of user code with no indication this is happening from directly reading the user code… that sounds pretty horrifying. I guess AOP in general is pretty horrifying to me though. Maybe it’s useful if you restrict its use to very specific things like logging or something.

poizan42 4 days ago

Well yes, hopefully you know what you are doing when you reference a source generator. This could ofc. also be done with custom msbuild task that modfies the code sent to the compiler or the assembly after compilation (like Fody), Source Generators just makes the process more streamlined and integrates with things like IntelliSense.

ninkendo 4 days ago

> Well yes, hopefully you know what you are doing when you reference a source generator

I don't think there's much that's scary about generating source code in general. If it's self-contained and you have to actually call the generated code to use it, it's not really much different than any other code. But the idea of having code A change the behavior of code B is what's horrifying, regardless of whether code A is generated or not. If I'm reading code B I want to be able to reason about what I see without having to worry about some spooky action at a distance coming from somewhere else.

jayd16 4 days ago

> that sounds pretty horrifying.

Things are constantly doing this. Frameworks use reflection or markup or all other kinds of things that count as magic if you don't bother to understand what's going on.

pjmlp 5 days ago

I am completly against them, I think they have re-invented Microsoft Fakes, and PostSharp, only badly.