Teaching Swift in a university course

I teach a course on language theory (formal languages, grammars, automata, etc.) by having students develop a programming language using a programming language that they are unlikely to have seen before. I currently use Rust, but I would like to switch to Swift for this semester.

Is it workable to have students develop Swift on Windows?

Workable to me means that I can get students to set up a code editor with support for:

  • Automatic formatting
  • Auto-completion and snippets
  • Debugging

I have tried to get Swift 5.1-dev working in WSL (Ubuntu 18.04), but I am having some problems.

  • The REPL does not work (might be related to Swift REPL does not start · Issue #4030 · microsoft/WSL · GitHub). When I type swift at the prompt the program hangs indefinitely. I can build, run, check the version, etc. just fine. This is not a huge loss.

  • SourceKit-LSP does not work. I installed using the directions on the GitHub page. It appears to run when I directly execute the program, though I see no output when running this way. But Visual Studio Code states that running the LSP has failed. I am reasonably certain that I have the plugin setup correctly, as when I have a setting wrong it gives a different error.

My topic here is related to:

Is it workable to have students develop Swift on Windows?

5 Likes

I think that more precision is needed on what you are trying to accomplish. WSL is the Ubuntu user space running under a NT kernel. From the perspective of Swift, it is just Linux, there is nothing specific to Windows there.

If you would like to use Swift on Windows, that is certainly possible. I think that with some help to get the experience to exactly what you would like, this should be largely possible. I think that some of the things can be improved, e.g. the toolchain installer can be improved, adding an installer for the SDK, etc. I think most of the work has already been explored and there is sufficient existing content there to make this a pretty simple task. I suppose at the very least, enumerating what items others are interested in seeing so that we have a checklist of items to resolve would also be beneficial.

There are binaries available at Azure DevOps Services | Sign In for the toolchain as well as the SDK which should allow you to run Swift on Windows. This includes lldb (sufficient to run the basic REPL), the swift compiler (sufficient to build the Swift standard library and core libraries for Android), as well as the "SDK" contents (the Swift pieces to build Swift code for Windows, Linux, and android).

The problem comes down to basically SourceKit-LSP - SourceKit has been ported, llbuild should build as well. The problem is that SourceKit-LSP depends on swift-package-manager's libBasic IIRC and also serves as the build system for SourceKit-LSP. I think that adding a CMake based build for SourceKit-LSP might make it easier to bring SourceKit-LSP to Windows which addresses your last point.

The piece which remains really unclear - do you want to use Swift on Windows or Swift on Linux under Windows? (Note that if it is the latter, another option is to use VMware Workstation or HyperV).

2 Likes

Thanks for the quick reply!

I agree that my question could have been more clear. I am mostly asking if Swift is mature enough to get working with WSL. I have no problem going through a manual setup process since I think doing so will be useful for my students anyway.

I guess what it comes down to is that I have been able to get Swift working with Visual Studio Code (including SourceKit-LSP) on a Linux computer, but I cannot get it to work following the same steps in WSL. This is either because I a missing something or because Swift is not quite at the point where it can be used the way I want it to be used.

I will checkout our your link on windows-swift. I thought that running Swift directly on Windows was a long shot so I haven't really checked it out.

I have been successfully coding swift on Windows this week using VSCode and compnerds windows-nightly build (which runs on Windows directly). I couldn't get SourceKit-LSP working so my ability to be productive relies on me already knowing Swift very well. Thankfully VSCode's swift syntax highlighting works fine. I invoke the swiftc command from powershell but an example of the kind of hindrance you encounter is that the swift compiler messages include lots of characters unrecognised by Powershell, but you can mostly understand what they mean.

e[1msrc\main.swift:16:6: e[0me[0;1;35mwarning: e[0me[1mresult of call to 'filter' is unused    
e[0m    .filter{ formatID -> Bool in
e[0;1;32m     ^     ~~~~~~~~~~~~~~~~~~~~~~

CC @svanimpe

Swift reuses LLVM's support for color diagnostic output, and it looks like it's generating VT100-style color codes even though it's outputting to a Windows console. Do LLVM and Clang support color in Windows terminals? Maybe Swift is missing setup code to configure its output correctly.

1 Like

IIRC, yes, clang does support color diagnostics on Windows. I think that we just aren't setting that up properly, but I've not yet looked into.

1 Like

Why not setup linux boxes for your students ? swift works there really nicely

I wouldn't recommend using Windows for the moment, the experience isn't nice, yet!, missing SPM mainly

And you can get educational licenses for CLion wich provides code completion / syntax highlighting and debugging for Swift

This seems like maybe not a good requirement for a programming class unless it's specifically oriented towards Linux development (although, I suppose that's ultimately between @ajc, the dean, and the school's IT dept).

That really depends. If the course is concerned with teaching GUI application development, then I'd agree that Linux is suboptimal, but I'd say that any flavor of UNIX or Linux would be better than Windows for a CLI programming course. Especially since you can just install Linux on Windows through WSL.

If it's GUI development, I'm not aware of a cross-platform option for Swift. Not one that wouldn't require going through a C layer, anyway.

On the CLI side, does Swift work on WSL? If so, yeah, that's probably the easiest solution I can think of for now. Well, maybe. Depends on how hard it is to run Visual Studio Code (or whatever editor) in the Linux layer... I haven't used WSL much, and not at all since the new version came out.

That's fine, as I'm not aware of a good cross platform GUI layer. The best you can hope for seems to be "good enough." And, yes, it worked fine in WSL last I checked.

I don't think that s-p-m missing means that the experience is terrible. Yes, a particular tool is unavailable, and it would be nice to get that ported as well, but it should be possible to do most things on Windows just fine.

AppCode + Linux + Swift

What steps are you using to get VS Code running under WSL? I haven’t tried it myself, but it seems like it should be possible to run GUI applications directly through WSL, in which case SourceKit-LSP and the rest of the Swift toolchain should work as per normal:

SPM is important piece, because that'll enable people working on crossplatform libs without frictions

Work on IDE with VSCODE/CLION will be easier since it's gonna be the same everywhere, same project model

Teaching will be easier since same experience no matter the OS

I don't want to teach cmake if i want teach swift, that's adding more burden and complexity to something that'll get dropped soon, and worse, could give bad impression of the language to the new people, CMAKE is not userfriendly at all

2 Likes

There are some

GTK :

QT:

LIBUI:

1 Like

CLion in action, experience is the same on Linux, and will be same on Windows once we get SPM

Since two of my threads have been linked and I've managed to set up an environment with WSL I'll give my impressions. Swift looks like one of the best languages to teach programming with, but unfortunately the environment is just way too new and it's not ready for use in windows through WSL in my experience. I have several years of work experience and was motivated to set it up and managed to do it on VS Code with WSL for the most part (with some help from this forum) but I've since abandoned the idea.

First you'll have to get them to install the latest unstable dev release of swift. Then you'll have to make them install lsp which is also in beta from what I can tell. This was not a fast or easy experience but it's doable and it worked kind of ok. Before I could try to set up debugging or run configurations my VS Code crashed my PC (with the blue :( face) when I switched to a new directory on the project window in VS Code with the remote interpreter. It now crashes every time I try to start VS code. I know this probably isn't swift's fault, but the point is this has been far from a pleasant experience and I can't imagine the frustration if this was one of my first programming classes in university. Be prepared for your share of frustration too as you'll be expected to resolve all these issues that students will surely find.

As much as I'd love it to be ready if it was me I'd just use python 3 for a beginner class right now and save myself and my students the frustration.

4 Likes

:point_up:

I'm hoping more people sharing their experiences like this will result in cross-platform support becoming more of a priority.

10 Likes