Skip to content
This repository has been archived by the owner on Dec 27, 2020. It is now read-only.

Init with @functionBuilder #2

Closed
ay42 opened this issue Jul 31, 2019 · 9 comments
Closed

Init with @functionBuilder #2

ay42 opened this issue Jul 31, 2019 · 9 comments

Comments

@ay42
Copy link
Member

ay42 commented Jul 31, 2019

Allow users to init Grid with function builder

Grid {
    Text("Item")
    Text("Item2")
}
@ay42
Copy link
Member Author

ay42 commented Aug 24, 2019

This is related to #8
If anyone figured out how to do this please let me know.

@ay42 ay42 removed their assignment Aug 24, 2019
@TilBlechschmidt
Copy link

Looks like this could be relevant:
https://developer.apple.com/documentation/swiftui/viewbuilder
Will investigate further.

@ay42
Copy link
Member Author

ay42 commented Aug 24, 2019

@TilBlechschmidt we were able to init it with function builder in previous release
https://github.com/spacenation/grid/blob/eafea07725c1172fe630d6724bd6edac06bef728/Sources/Grid/Grid.swift#L10-L14
But the difficulty here is to process view builder directly like so:

public init(@ViewBuilder content: () -> Content) { ? }
Grid {
    Text("1")
}

@ay42 ay42 removed this from the Version 1.0.0 milestone Aug 25, 2019
@hmerzin
Copy link

hmerzin commented Nov 12, 2019

Hi! I would love to take this issue. Any advice on a good place to start?

@ay42
Copy link
Member Author

ay42 commented Nov 13, 2019

@hmerzin Hi! This is as far as I have been able to take it

extension Grid {
    public init<Data: RandomAccessCollection, ID, Item: View>(@ViewBuilder content: () -> Content) where Content == ForEach<Data, ID, Item> {
        let views = content()
        self.items = views.data.map { AnyView(views.content($0)) }
    }
}

This allows to init grid like so:

Grid {
    ForEach(...)
}

I honestly have no idea how to achieve the behaviour of native elements like List, Group, HStack.
There it is possible to just do this and it works:

HStack {
    Text("1")
    Text("2")
}

It would be awesome if you can figure this out.

@hmerzin
Copy link

hmerzin commented Nov 13, 2019

as luck would have it, I recall reading a blog post a few weeks agothat elaborates on function builders and how one might be able to harness that syntax, though I am not entirely sure if it is possible outside of a SwiftUI context, as I remember seeing a constraint on the syntax that I thought was a bit strange. Nevertheless, I will take a look and let you know what I find and if I can prototype a solution!

thanks,
Harry

@ay42
Copy link
Member Author

ay42 commented Nov 14, 2019

@hmerzin Looks there is a way to do this (#29). It's not perfect but it's an API we are looking for. Is this something similar to what you wanted to implement? Let me know

@hmerzin
Copy link

hmerzin commented Nov 14, 2019

@ay42 using @ViewBuilder was exactly the syntax that I was exploring for implementing this. I think I am pretty close, but basically I have just been fiddling with the initializer for HStack

@ay42
Copy link
Member Author

ay42 commented Nov 17, 2019

#31 has improvements that address this issue as well.
I feel like we can close this.

@ay42 ay42 closed this as completed Nov 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants