chkn / Xamarin.SwiftUI

.NET bindings for SwiftUI
MIT License
98 stars 7 forks source link

[C#] Add sugar for State properties #42

Open chkn opened 3 years ago

chkn commented 3 years ago

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.

nitz commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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.