Skip to content

Defining methods on Updates<MySelection> #273

Description

@rcarver

Hey @stephencelis, we made some fixes for @Selection in 0.26.0 and somehow I didn't end up updating the app to use them. I'm doing that now and hit a snag.

From that change:

This is the simplest way I could come up with to fix the nested update issue. Underscored type for now since I'm not sure folks should be using it, especially if we come up with a better way to handle things in the future. But if we come up with more use cases for this type, we could consider making a proper public API.

Originally posted by @stephencelis in #233

The issue is that I've defined methods on selection types in the same way that you'd do so for table types, via Updates<MyTable> for example:

@Selection struct NestedFields {
  var honestCount: Int = 0
  var optionalCount: Int?
}

extension Updates<NestedFields> {
  mutating func modify(count: Int) {
    self.honestCount = count
    self.optionalCount = Optional(count)
  }
}

@Test func mutateSelectionFields() {
  assertInlineSnapshot(
    of: Root.update {
      $0.fields.modify(count: 4)
    },
    as: .sql
  )
}

Now it looks like I'd have to define it like this:

extension Updates<TableAlias<NestedFields, _TableAliasName<Root>>> {
  mutating func modify(count: Int) {
    self.honestCount = count
    self.optionalCount = Optional(count)
  }
}

Which is unfortunate since now modify(count:) can only be used via Root and not other embedding types. We can clean up the syntax like this, but the coupling of selection+root remains.

public typealias UpdatesOf<S, B> = Updates<TableAlias<S, _TableAliasName<B>>> where S: Table & QueryExpression, B: Table

So I guess the question is: it possible to define selection updates on a type with one generic?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions