I'm playing around with InlineArray now that the kind folks helped me to get it working and I ran into some challenges in usability. For context, I'm making a postgresql extension which should run in the order of microseconds – hence the optimizations.
What I'd like to do is this:
query<3>("SELECT some, sql, columns FROM wherever")
... where 3 is the number of columns selected. Let's start with that simple case.
To define this function I need something like this:
func query<let N: Int>(_ query: StaticString) -> AnyIterator<InlineArray<N, PGDatum>> {
...
return AnyIterator {
let row = InlineArray<N, PGDatum> { ... }
return row
}
}
The issue is, that doesn't build. It's not allowed to specify 3 in query<3>("...").
What I can do instead is call query as let result: AnyIterator<InlineArray<3, PGDatum?>> = query(...) – but that's not very usable.
I then considered the option of using an InlineArray to specify the columns in the SQL statement: query("SELECT", ["some", "sql", "columns"], "FROM ...")
That in theory allows me to use type inference to get the number of columns – even better! But then I'm stuck with the issue of StaticString – there's no reasonable way to concatenate them at compile time. So I'd be stuck with concatenating them at runtime for every iteration of the query – that's probably worse than simply using Array in the first place.
Any ideas?