Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 10:45
    dotnet-issue-labeler[bot] labeled #57297
  • 10:45
    dotnet-issue-labeler[bot] labeled #57297
  • 10:45
    shuebner opened #57297
  • 10:30
    feeleen commented #49547
  • 10:28
    feeleen commented #49547
  • 09:50
    CyrusNajmabadi commented #49547
  • 09:45
    feeleen commented #49547
  • 09:42
    feeleen commented #49547
  • 09:19
    CyrusNajmabadi commented #49547
  • 09:09
    feeleen commented #49547
  • 08:59
    CyrusNajmabadi commented #49547
  • 08:53
    CyrusNajmabadi commented #49547
  • 08:51
    CyrusNajmabadi commented #49547
  • 08:45
    CyrusNajmabadi commented #49547
  • 08:43
    feeleen commented #49547
  • 08:40
    CyrusNajmabadi commented #49547
  • 08:30
    feeleen commented #49547
  • 08:29
    feeleen commented #49547
  • 08:28
    feeleen commented #49547
  • 08:24
    dagood commented #57165
arturohernandez10
@arturohernandez10

@CyrusNajmabadi
Sure...

        public static void Original()
    {
        var attributes = new [] { new { Name = "Country", Include = false }};
        var dict = new Dictionary<string,string>(){{"Country", "USA"}};
        var list = new List<string>();

        foreach (var attribute in attributes)
            if (attribute.Include)
                list.Add(dict[attribute.Name]);                
    }

    public static void Expanded()
    {
        var dict = new Dictionary<string,string>(){{"Country", "USA"}};
        var list = new List<string>();

        list.Add(dict["USA"]);                
    }
}

It's a somewhat contrived example. In general, you have a variable like attributes in this case, whose entire contents are known at compile time. Expanding only within a limited scope, may be a tractable problem. I clearly used foreach rather than whereas part of limiting the scope. Many coding patterns could be expressed in a similar way, while keeping a good amount of type and syntax checking. And of course this would be useful in code generation.

CyrusNajmabadi
@CyrusNajmabadi
sure. you could use source-generators to solve the above.
look for whatever pattern you want, and generate the final form as-app
arturohernandez10
@arturohernandez10
@CyrusNajmabadi Do you know of any previous, useful or similar work. I'd rather not reinvent the wheel.
CyrusNajmabadi
@CyrusNajmabadi
not really...
there are likely lots of source generators you could look at
but your use case is pretty specific
but would be very feasible with the source generator (and other) roslyn apis.
arturohernandez10
@arturohernandez10
It's the partial evaluation that I could use, even if it's just a reference to a research paper. I do have an idea on how to do this. But I always do a little bit of research. Is this the best place to ask?
CyrusNajmabadi
@CyrusNajmabadi
no, i know of nothing in roslynt hat would help with that.
this is a fine place to ask :)
Yair Halberstadt
@YairHalberstadt
Here's a simple hacky way to do it if the result is a constant. The user marks a static method as CompileTimeEvaluated using an attribute. The SG takes only that method and copies it into a new compilation which has the same metadata references as the previous compilation. It emits that compilation as an assembly and runs that method. It then puts the result in a property somewhere.
That would work reasonably well if the result is any serializable type, but deserialization is likely to be more expensive than just running the method at runtime anyway.
arturohernandez10
@arturohernandez10
@YairHalberstadt Thanks a lot!! I am really going after extracting patterns from codebases. I would like to have a Pattern project, similar to Test project. Just like you can do a Partial evaluation you could take an existing piece of code. Give it a generic interface and using generic types you could define the pattern with a lower abstraction cost. ;)
Yair Halberstadt
@YairHalberstadt
I'm not sure i understood to be honest
arturohernandez10
@arturohernandez10

@YairHalberstadt
Yes, here is an example:

    public static TOut Pattern<TIn,TOut>(Attributes attributes) => (TIn input) =>
    {
        var result = new TOut();

        foreach (var attribute in attributes)
            if (attribute.IsEnum())
                attribute.Assign(result, input);

        return result;
    }

    public static Person Example(Student person)
    {
        var result = new Person();

        result.Degree = __.GivenField(person.Degree.ToString(), a.IsEnum());

        return result;
    }

In sample we can use an example object (not to write generic). __.GivenField Is just a type preserving function, which also has room for an expression tree. The expression tree is used to determine the kind of code generation to be used. And would match the pattern above. The idea is that the pattern implementation code, will always be harder to read. While the example code should be much easier. I think this could be a good thing to try.

I would like to generate the patterns from example decorated code. And that could be a second step. I like the idea of writing a quick first implementation even if the code is ugly.
Авдеев Вячеслав
@lsoft
so, with vs2022 preview4 incremental generators are going in the wild))) but no docs and examples exists :( (except for single not completed md file)...
arturohernandez10
@arturohernandez10
@lsoft do you have a link to that single md file?
2 replies
Leandro T. C. Melo
@ltcmelo
hi everyone, I'd like to share that we've made public a GH repo with our Roslyn-based syntax rewriter tool for C#
I don't think that it's useful for Roslyn itself, but it may be useful to people creating Roslyn-based tools
I appreciate if you chould re-share with anyone you believe might be interested :-)
thanks!
CyrusNajmabadi
@CyrusNajmabadi
ooh, this looks cool
i will ahve to look at those tests to see what htey all do :)
Leandro T. C. Melo
@ltcmelo
don't look too close :-P
joking... feedback/critique from you would be highly appreaciated @CyrusNajmabadi
some rewriters are better tested than others
and their "coverage" is pretty much driven by the diversity of code we've encountered in the field (at this point, we've accumulated a number of POCs and customers)
e.g., you won't see proper handling of co/contravariance in generics, as we don't bump into them with frequency
and, I only have so much time and need to prioritize :-)
Bernd Baumanns
@bernd5
thanks for sharing
Bernd Baumanns
@bernd5
I tried once to write a linq-rewriter, too.
I tried your code - and it fails at the same point
Bernd Baumanns
@bernd5
the problem is the shadowing of thing
it would be great if that would work, too.
BTW.: this is the most complex linq query I have ever seen...
Bernd Baumanns
@bernd5
If you translate it you get: System.ArgumentException: "An item with the same key has already been added. Key: ____TRANSPARENT2"
Bernd Baumanns
@bernd5
I took the wrong sample
but both fail
arturohernandez10
@arturohernandez10
@ltcmelo Awesome!!!
still, feel free to create an GH issue if you'd like
thanks @arturohernandez10
Atul Sharma
@atksharma_twitter
Folks - For some reason , After updating to the latest version of roslyn dependencies, the documents are not loading inside the solution after opening the workspace .. Any body else facing this issue ?

Folks - For some reason , After updating to the latest version of roslyn dependencies, the documents are not loading inside the solution after opening the workspace .. Any body else facing this issue ?

These are the version of nugets we are currently using - -Microsoft.Build.Locator 1.4.1
Microsoft.CodeAnalysis.Analyzers 3.3.2
Microsoft.CodeAnalysis.CSharp 3.11.0
Microsoft.CodeAnalysis.CSharp.Workspaces 3.11.0
Microsoft.CodeAnalysis.Workspaces.MSBuild 3.11.0

Hugh Gleaves
@Korporal
VS has always been limited when it comes to being able to rename namespaces, I wonder if VS 2022 will offer improvements in this area...
2 replies