I am trying to use Swift in a Jupyter Notebook with the following Kernel: GitHub - rayh/iSwift: A Swift kernel for IPython.. This kernel seems to use the Swift REPL to evaluate the user's code. That works well in some cases, but there are a few problems. For example, consider the following example:
Thanks for your response! I think that could be a possible solution. My Jupyter notebook contains Swift exercises that consist of multiple steps each of which is displayed in a separate cell. If I wrap each cell in a do {} block, I can't access variables / functions / types that were declared in a previous cell. But maybe I just have to use one larger cell for each exercise that contains all the steps for that exercise. With the do {} block I could then at least execute the cell multiple times.
Another issue that I see with this approach is that some things (e.g., extensions, imports, operator declarations) need to be declared at file scope. But I can probably avoid those features in my use case.
do {
struct S {
var x: Int
static func +(lhs: S, rhs: S) -> S {
return S(x: lhs.x + rhs.x)
}
}
let s1 = S(x: 1)
let s2 = S(x: 2)
print(s1 + s2) // error: binary operator '+' cannot be applied to two 'S' operands
}
When you overload an operator for a local type the compiler can't find the corresponding operator function. Seems like the == operator only works because of the Equatable auto-synthesis:
do {
struct S: Equatable {
static func ==(lhs: S, rhs: S) -> Bool {
return false
}
}
print(S() == S()) // true (but should be false)
}