Search code examples
swiftlinuxfedora

Modules declared in files Swift project on Linux


I'm trying to start a new Swift project for some training purposes on Fedora 30.0. The project consists of 2 simple files:

main.swift

let req: Request = RequestImpl()
req.sendRequest(url: "hello")

Request.swift

protocol Request {
    func sendRequest(url: String)
}

final class RequestImpl: Request {

    // MARK: - Request

    func sendRequest(url: String) {
        print(url)
    }
}

Problem

When I run the command swiftc main.swift on the terminal, I get the following error:

$ swiftc main.swift 
main.swift:1:10: error: use of undeclared type 'Request'
let req: Request = RequestImpl()
         ^~~~~~~

P.S: I run swiftc Request.swift before I run the above command.


Solution

  • This should compile, but print doesn't work:

    $ swiftc Request.swift main.swift

    If you want create module with multiple files and run it, I will suggest create executable package, build and run it.