Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Union] - Code generation not generating anything #770

Closed
MrYossu opened this issue Apr 27, 2020 · 12 comments
Closed

[Union] - Code generation not generating anything #770

MrYossu opened this issue Apr 27, 2020 · 12 comments

Comments

@MrYossu
Copy link
Contributor

MrYossu commented Apr 27, 2020

Hello,

Update Please read my second post here, as I think the problem I describe in this post is a result of a more fundamental problem.

I'm playing with LanguageExt.CodeGen and created a Core 3.1 console app project, added references to LanguageExt and LanguageExt.CodeGen and then copied the code from the DU section of the wiki into the Program.cs file.

However, this gives compiler errors in the GetArea method, claiming that the Rectangle and Circle symbols cannot be resolved.

Any ideas what I did wrong? Here is the whole code file...

using System;
using LanguageExt;

namespace LanguageExt_CodeGen {
  internal class Program {
    private static void Main(string[] args) {
      Console.WriteLine("Hello World!");
    }

    // you can use C# pattern matching like F#
    public double GetArea(Shape shape) {
      return shape switch {
        Rectangle rec => rec.Length * rec.Width,
        Circle circle => 2 * Math.PI * circle.Radius,
        _ => throw new NotImplementedException()
      };
    }
  }

  [Union]
  public interface Shape {
    Shape Rectangle(float width, float length);
    Shape Circle(float radius);
    Shape Prism(float width, float height);
  }
}

As a secondary question, how would I create a Rectangle or `Circle``? I can't find any samples that show how to work with DUs.

Thanks

@MrYossu
Copy link
Contributor Author

MrYossu commented Apr 28, 2020

OK, I think I'm doing something fundamentally wrong here. I just replaced the code above with the second code snippet he showed (for Maybe), compiled it and used both ILSpy and JustDecompile to see what was compiled, and in both cases, I got very little code, basically only what I added, nothing generated...

// LanguageExt_CodeGen.Maybe<A>
using LanguageExt_CodeGen;

public interface Maybe<A>
{
	Maybe<A> Just(A value);

	Maybe<A> Nothing();
}

LanguageExt_CodeGen is my (probably poorly chosen)project name.

Compared to the generated code he linked to in the wiki article, there is next to nothing here, which is probably why I'm struggling.

So, anyone any idea why I'm not getting code generated? Thanks

@MrYossu MrYossu changed the title Compiler error trying to use [Union] [Union] - Code generation not generating anything May 4, 2020
@garyng
Copy link

garyng commented May 14, 2020

I think the codegen user experience would be much nicer once #754 is merged. Previously the package need a very specific project structure to make it work, now just install the package and you will be good to go.

@MrYossu
Copy link
Contributor Author

MrYossu commented May 14, 2020

@garyng Thanks for the reply. I'm a bit confused though, you say "once #754 is merged", implying that it hasn't been yet, but then say "now just install..." implying that it is already merged. Please can you clarify.

Also, what specific project structure do I need to make it work in the meantime? I tried a few different things, including both .NET Standard and Core, but none of them worked.

Thanks again.

@garyng
Copy link

garyng commented May 14, 2020

@MrYossu sorry for the confusion, the process will be much easier after that PR is merged.

From my own experiment, the current codegen can only be installed in a netcoreapp project, but not in netstandard dll project. And your classes (to be codegen-ed) must be inside the netcoreapp project as well (that is, the codegen can only generate code for the classes in the current netcoreapp project).

After carefully reading your previous comments, seems like you are using the correct project structure. Did you try restarting visual studio and rebuilding the whole solution? Sometimes visual studio won’t pickup the generated code.

@MrYossu
Copy link
Contributor Author

MrYossu commented May 14, 2020

@garyng Thanks for the clarification. I did try it in Core, and the classes were in that project, so it should have worked. I'm pretty sure I restarted VS a few times during the course of my experiments, but will try again. If I still can't make it work, I'll post a sample and maybe you would be kind enough to take a look.

Thanks again.

@garyng
Copy link

garyng commented May 14, 2020

@MrYossu Yeah sure! Upload the project that’s not working for you maybe, I can take a look when I get home.

@ffgiraldez
Copy link

ffgiraldez commented May 19, 2020

I recently archive work on a netstandard 2.1 lib project with

<ItemGroup>
        <PackageReference Include="LanguageExt.CodeGen" Version="3.4.14" PrivateAssets="all" />
        <PackageReference Include="LanguageExt.Core" Version="3.4.14" />
        <DotNetCliToolReference Include="dotnet-codegen" Version="0.6.1" />
    </ItemGroup>

I found problems with .Net 3.1 that does not reference dotnet-codegen and needs the 2.1 in order to make it compile(that should go away once #754 get merged), but once it work It generate all the stuff.

also in order to instantiate an element of the Unition you should do it via static builder, {UnionType}Con.{BranchTyppe}()

in your scenario in order to instantiate a Circle should be ShapeCon.Circle(0.5f)

@ffgiraldez
Copy link

also notice that even that inside obj\Debug\netstandard2.1\ I could see the generated file correctly until I not add this

<ItemGroup>
        <Compile Update="MyFileWithCodeGen.cs">
            <Generator>MSBuild:GenerateCodeFromAttributes</Generator>
        </Compile>
</ItemGroup>

Intellisence does not notice about newly generated classes

@MrYossu
Copy link
Contributor Author

MrYossu commented May 19, 2020

@ffgiraldez Thanks for the reply. I tried again, using 2.0 (as I don't have 2.1 installed), but didn't have any more luck.

I zipped up a small sample project, which you can download from here. Please could you take a look and see what I'm doing wrong.

Thanks again.

@ffgiraldez
Copy link

@MrYossu you have misconfigured csproj file, as I pointed before, you should have

 <ItemGroup>
        <DotNetCliToolReference Include="dotnet-codegen" Version="0.6.1"/>
        <PackageReference Include="LanguageExt.CodeGen" Version="3.4.12" PrivateAssets="all"/>
        <PackageReference Include="LanguageExt.Core" Version="3.4.13"/>
    </ItemGroup>

but instead you have

 <ItemGroup>
    <PackageReference Include="CodeGeneration.Roslyn.Tool" Version="0.7.63">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="LanguageExt.CodeGen" Version="3.4.12" />
    <PackageReference Include="LanguageExt.Core" Version="3.4.13" />
  </ItemGroup>

the key part is

<PackageReference Include="CodeGeneration.Roslyn.Tool" Version="0.7.63">
    ...
</PackageReference>

when #754 is merged you could use Roslyn version 0.7.63 until that you should use the version 0.6.1 as mentioned in the wiki

@MrYossu
Copy link
Contributor Author

MrYossu commented May 19, 2020

@ffgiraldez Ah, silly me, I missed that part! With your change, the code is generated.

However, ShapeCon isn't recognised until I restart VS. Is there any workaround for this? It's going to be painful having to restart every time I make a change.

Thanks again for all your help

@ffgiraldez
Copy link

also notice that even that inside obj\Debug\netstandard2.1\ I could see the generated file correctly until I not add this

<ItemGroup>
        <Compile Update="MyFileWithCodeGen.cs">
            <Generator>MSBuild:GenerateCodeFromAttributes</Generator>
        </Compile>
</ItemGroup>

Intellisence does not notice about newly generated classes

@MrYossu have you include that? I usually build once before start using the generated helpers, also o. Rider I don't have that problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants