Friday, April 8, 2022

[SOLVED] Modules declared in files Swift project on Linux

Issue

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

class="lang-swift prettyprint-override">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.



Answered By - Michcio
Answer Checked By - David Marino (WPSolving Volunteer)