编译器在编译多个文件时无限期挂起 - Swift 5.5 版本

Compiler hangs indefinitely while compiling multiple files - Swift 5.5 Release

如问题所述,编译和 运行 下面的 2 个文件会使编译器无限期挂起。以下是我尝试过的 CLI 命令: swiftc *.swift -o combined && ./combinedcat *.swift | swift -.

目录中仅有的 2 个 swift 文件是 main.swiftVehicle.swift

我试过分别编译和运行这两个文件,Vehicle.class没有错误并且编译正确,main.swifterror: cannot find 'Vehicle' in scope错误,这是可以理解的,因为我需要将它们编译在一起。编译器没有其他反馈。什么会导致编译器挂起?

代码:

main.swift

// Stub program to demonstrate the Vehicle class
let vehicle1 = Vehicle(newNumOfDoors: 4, newMaxSpeed: 150,
    newLicensePlate: "ASDF404", newColour: "Red")

vehicle1.licensePlate = "FGHJ968"
vehicle1.colour = "Green"
vehicle1.accelerate(accelerateBy: 60)

print("\nLicense Plate: " + vehicle1.licensePlate
    + "\nColour: " + vehicle1.colour
    + "\nNumber of Doors: " + vehicle1.numOfDoors
    + "\nMax Speed: " + vehicle1.maxSpeed
    + "\nCurrent Speed: " + vehicle1.speed)

Vehicle.swift

public class Vehicle {

    // Properties Declaration
    let numOfDoors: Int
    let maxSpeed: Int
    private(set) var speed: Int
    var licensePlate: String
    var colour: String

    // Initializes a Vehicle
    init (newNumOfDoors: Int, newMaxSpeed: Int,
          newLicensePlate: String, newColour: String) {

        self.numOfDoors = newNumOfDoors
        self.licensePlate = newLicensePlate
        self.maxSpeed = newMaxSpeed
        self.colour = newColour
        self.speed = 0
    }

    func accelerate(accelerateBy: Int) {
        self.speed += accelerateBy
    }

    func brake(brakeBy: Int) {
        self.speed -= brakeBy
    }
}

编译器与您的 print 语句有问题(我通过从程序中删除元素直到它起作用来确定)。这可能是因为在使用 + 连接所有内容时很难弄清楚类型。

一个选项是使用多行字符串文字:

let vehicle1 = Vehicle(newNumOfDoors: 4, newMaxSpeed: 150,
    newLicensePlate: "ASDF404", newColour: "Red")

vehicle1.licensePlate = "FGHJ968"
vehicle1.colour = "Green"
vehicle1.accelerate(accelerateBy: 60)

let str = """
License Plate: \(vehicle1.licensePlate)
Colour: \(vehicle1.colour)
Number of Doors: \(vehicle1.numOfDoors)
Max Speed: \(vehicle1.maxSpeed)
Current Speed: \(vehicle1.speed)
"""

print(str)

另一种选择是使用插值而不是 +:

print("\nLicense Plate: \(vehicle1.licensePlate)"
    + "\nColour: \(vehicle1.colour)"
    + "\nNumber of Doors: \(vehicle1.numOfDoors)"
    + "\nMax Speed: \(vehicle1.maxSpeed)"
    + "\nCurrent Speed: \(vehicle1.speed)")