FastScriptReload icon indicating copy to clipboard operation
FastScriptReload copied to clipboard

Try to fix rewrite for self file extension method [Workaround]

Open builder-main opened this issue 1 year ago • 4 comments

Hi, let's try to make a rewriter for extension methods. It would allow to avoid ambiguous call compilation errors :

public static class SomeClass {

  public static void MyExtension(this string myString){  ... }

  public static void MyMethod(){  "someValue".MyExtension() } //Will cause an ambiguous call in the compiler

  public static void MyMethod(){  MyExtension("someValue") } //This works and is the current workaround.
}

builder-main avatar Jan 17 '24 21:01 builder-main

Thanks - I think extensions are also part of other feature. Off top of my head there were some more issues with that unfortunately.

I think especially for rewritten code where YourType would end up YourType__Patched_ and extension method will complain.

We could probably cast it to (ProperType)(dynamic)variable. That's a hack for compiler as your variable is still of old type, same approach is used for something else already (I think for 'this' rewritting)

handzlikchris avatar Jan 22 '24 09:01 handzlikchris

Is it not possible for rewriting extension methods yet now? Currently I’m working with one ECS framework of Unity, and it uses extension method almost everywhere :(

BillDong2021 avatar May 15 '24 15:05 BillDong2021

sorry, I've not had a chance to get to it. I could point you in the right direction if you want to give that a try

handzlikchris avatar May 21 '24 11:05 handzlikchris

Is it not possible for rewriting extension methods yet now? Currently I’m working with one ECS framework of Unity, and it uses extension method almost everywhere :(

There is the workaround in the meantime with classical static method syntax. Making a specific rewriter is a feasible task even for newcomers, you could look at the examples and try make a PR if this is a subject that is bothering you a lot.

builder-main avatar Aug 31 '24 10:08 builder-main