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

[C#] Add sugar for State properties #42

Open
chkn opened this issue Dec 2, 2020 · 5 comments
Open

[C#] Add sugar for State properties #42

chkn opened this issue Dec 2, 2020 · 5 comments
Labels
lang: C# Specific to the C# API

Comments

@chkn
Copy link
Owner

chkn commented Dec 2, 2020

Ideally, I'd like to achieve a syntax like this:

partial record MyView : View {
    [State] int Count { get; set; } = 0
    public View Body => Button ($"Clicked {Count} times", () => Count++);
}

Under the covers it would be expanded to this:

partial record MyView : View {
    readonly State<int> _Count = new State<int> (0);
    [State] int Count {
        get => _Count.Value;
        set => _Count.Value = value;
    }

    public View Body => Button ($"Clicked {Count} times", () => Count++);
}

Unfortunately, I don't think source generators are flexible enough for this currently.

@chkn chkn added the lang: C# Specific to the C# API label Dec 2, 2020
@nitz
Copy link

nitz commented Aug 25, 2021

Hello! I just stumbled upon your project today. I started playing with SwiftUI and found myself thinking "gosh I wish I could write this in C#", and thought to take a peek as to what's out there. Was just poking through the issues to see how active your project was, and found this one in particular.

I was thinking when looking at the readme that "I bet they wish it was just an attribute instead of having this whole state class to create", so it was fun to see that you definitely had the idea too.

There's a solid chance you've already considered it, but in lieu of source generators, have you thought about implementing sugar like that as a Fody weaver? I'm not quite versed on the internals of how it does the IL rewriting, but, it seems like it'd be a very similar thing to how they this Lazy addin works, generating code based on the property adorned with the attribute.

Anywho, cheers and keep up the great work!

@MrGreger
Copy link

Hello! I just stumbled upon your project today. I started playing with SwiftUI and found myself thinking "gosh I wish I could write this in C#", and thought to take a peek as to what's out there. Was just poking through the issues to see how active your project was, and found this one in particular.

I was thinking when looking at the readme that "I bet they wish it was just an attribute instead of having this whole state class to create", so it was fun to see that you definitely had the idea too.

There's a solid chance you've already considered it, but in lieu of source generators, have you thought about implementing sugar like that as a Fody weaver? I'm not quite versed on the internals of how it does the IL rewriting, but, it seems like it'd be a very similar thing to how they this Lazy addin works, generating code based on the property adorned with the attribute.

Anywho, cheers and keep up the great work!

Hey, I'll try implementing weaver for this feature in a separate nuget this weekend!

@chkn
Copy link
Owner Author

chkn commented Aug 27, 2021

Hello! I just stumbled upon your project today ...

Glad you stumbled on us! The IL weaver is a decent idea. I think I was holding out to see what could be done with a source generator because it's much simpler to integrate with the build and tooling.

Hey, I'll try implementing weaver for this feature in a separate nuget this weekend!

Awesome! Let me know if you have any questions

@MrGreger
Copy link

MrGreger commented Aug 27, 2021

https://github.com/MrGreger/SwiftUI.Postsharp

I made the Nuget package, it works for .net standard 2.1 projects, but I can't check how it works in xamarin. ios / xamarin.mac projects

@chkn could you check how it works for xamarin projects?

@chkn
Copy link
Owner Author

chkn commented Aug 27, 2021

@MrGreger This is great, thanks! Regarding Xamarin projects, I think we should just drop support for those and move to .NET 6. I've created #47 and will look into updating the build over the weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang: C# Specific to the C# API
Projects
None yet
Development

No branches or pull requests

3 participants