Lesson 13 (2019) discussion and wiki

I might just need to play around with this, but do you pass which python to use and which environment to use with Swift?

@saeta, @sgugger
That makes lots of sense. Thank you both.

Right, but they could have different implementations if they are of different types, no?

See Marc’s answer up here.

1 Like

I don’t see how: you have func + (_ lhs: Type1, _ rhs: Type2)

1 Like

Operators are declared as static functions. Perhaps a (dummy) example will make things clear:

struct IntWrapper {
  let value: Int

  static func + (lhs: IntWrapper, rhs: IntWrapper) -> IntWrapper {
    return IntWrapper(value: lhs.value + rhs.value)
  }
}

let x = IntWrapper(value: 1)
x + x // you can think of this like `IntWrapper.+(x, x)`

Operators are not commutative by default.
If you only declare static func + (lhs: A, rhs: B), then b + a wouldn’t work (where b is an instance of B and a is an instance of A).
But you can easily declare static func + (lhs: B, rhs: A).

4 Likes

Instead of calling a.__add__(b), you’re calling func +_for_Int_and_Int, or func +_for_Float_and_Float. Check out the "implementation of Float" in Swift for what this actually looks like at the lowest levels.

2 Likes

What is the URL for that code to X86 assembler language website?

1 Like

Nope. When you ask jupyter to execute a cell, it sends just that cell through the compiler and then immediately executes the resulting code.

2 Likes

That is likely to be highly subjective but, given that Swift is not completely developed for Jupyter notebook, I would love to know S4TF team’s thoughts on good IDE to start working with Swift.

1 Like

https://www.youtube.com/watch?v=nA4T8sCPWtg&list=PL5Q2soXY2Zi8J58xLKBNFQFHRO3GrXxA9 for folks who want to go beyond assembly :wink:

PLEASE don’t edit that fire drill out! :slight_smile:

1 Like

https://godbolt.org/ It has support for Swift, Rust, C++, and a number of other languages. Be sure to turn on optimizations if you want to see optimized code! :slight_smile: The example used in the course is at: Compiler Explorer

2 Likes

Can Swift/LLVM implement instructions to execute on the GPU? Would this ever be a good idea?

1 Like

Can you mix types in an array in swift?

Arrays have a specific type. But that type could be an “Existential Type” (such as Any) which naturally contains any type. :slight_smile:

2 Likes

Can we make a new style guide for using emojis in code? :smiley:

2 Likes

please correct me if I am wrong, but this is only intended for use as part of an ipython/jupyter notebook, right? Or can I use this S4TF stack from “terminal”? Can this be run as part of a script?

You can use CUDA to generate programs that execute on GPUs (which uses a low-level language very similar to LLVM). In the future, we expect to have very powerful compiler infrastructure (see XLA & MLIR) that can generate efficient programs for accelerator hardware.

1 Like