yaslab / CSV.swift

CSV reading and writing library written in Swift.
MIT License
661 stars 78 forks source link

2.5.0 Changes to CSVWriter.write(row:_) Break Swift Compiler #126

Open mgallegly opened 2 months ago

mgallegly commented 2 months ago

Changes made as part of the "Make CSVWriter.write(row:_) generic to accept any Sequence by @bouk in https://github.com/yaslab/CSV.swift/pull/113" change cause issues with the Swift Compiler.

The below code builds fine using CSV version 2.4.2 but fails using the new 2.5.0 version; with this error:

"The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions"

guard let stream = OutputStream(toFileAtPath: NSString("~/somefile.tsv").expandingTildeInPath, append: false) else { return }
let csvWriter = try CSVWriter(stream: stream, delimiter: "\t")

let v1: Int? = nil
let v2: String? = nil
let v3: String? = nil
let v4: String? = nil
let v5: String? = nil
let v6: String? = nil
let v7: String? = nil
let v8: String? = nil
try csvWriter.write(
    row:
        [
            v1 != nil ? String(v1!) : "",
            v2 ?? "",
            v3 ?? "",
            v4 ?? "",
            v5 ?? "",
            v6 ?? "",
            v7 ?? "",
            v8 ?? ""
        ]
)
yaslab commented 2 months ago

@mgallegly

Please try specifying the type of the Array's Element. For example...

try csvWriter.write(
    row:
        [
            v1 != nil ? String(v1!) : "",
            v2 ?? "",
            ...
        ] as [String] // Specifies the type of Element
)

Or, try making the array a variable. For example...

let row = [
    v1 != nil ? String(v1!) : "",
    v2 ?? "",
    ...
]

try csvWriter.write(row: row)