Home > front end >  Swift 5 - Error while assign property values of equal types through generic
Swift 5 - Error while assign property values of equal types through generic

Time:09-17

class SQS_Record {
   var table: SQS_Table? = nil
}

class SQS_Table<RecordType: SQS_Record> {

   func newRecord() -> RecordType {
      let new = RecordType()
         new.table = self               <<< ERROR: 
         return new
      }
}

COMPILER ERROR: Cannot assign value of type 'SQS_Table' to type 'SQS_Table<SQS_Record>'

In future i wish:

class Project: SQS_Record {
   var name: String = ""
}

let projectsTbl = SQS_Table<Project>(...)

and create object like this

let item = projectsTbl.newRecord()

CodePudding user response:

Given what you've described, you're looking for protocols, not classes. For example:

// TableRecords know their table (and know other things)
// Note that TableRecord is a PAT (protocol with an associated type).
// There can not be any variables of type "TableRecord." It can only
// be used to constrain other types (as in Table below).
protocol TableRecord {
    init(table: Table<Self>)
    var table: Table<Self> { get }
}

// Project is a concrete TableRecord that also has a name.
// There can be variables of type Project.
struct Project: TableRecord {
    let table: Table<Self>

    var name: String = ""
    init(table: Table<Self>) {
        self.table = table
    }
}

// Tables can create new records of the Record type
class Table<Record: TableRecord> {
    func newRecord() -> Record {
        let new = Record(table: self)
        return new
    }
}

let projects = Table<Project>()
let item = projects.newRecord() // item is of type Project

CodePudding user response:

Impossible to use generic type in recursive reference between two classes with inheritance Whats why we need to do:

  1. var table: AnyObject? - field can store any class (ref)
  2. (table as! SQS_Table).reloadRecord(record: self as! Self) - exotic type cast based on Self.

I cant solve this problem with Protocol. Inheritance from reference type (Class - NOT struct) and type cast - prohibited

class SQS_Record {
   var table: AnyObject? = nil

   // Example - access to referenced table method and pass self param
   func reload() {
      guard __table != nil else { return }
      (table as! SQS_Table<Self>).reloadRecord(record: self as! Self)
   }

}

class SQS_Table<RecordType: SQS_Record> {

   // RecordType - subclass of SQS_Record

   func newRecord() -> RecordType {
      let new = RecordType()
         new.table = self               
         return new
      }

   func reloadRecord(record: RecordType) {
      ...
   }
}

class Project: SQS_Record {
   ...
}

let projectsTable = SQS_Table<Project>(...)

let newProjectRecord = projectsTable.newRecord()

newProjectRecord.reload()
  • Related