0

I wanted to check if it is necessary to compile the code before using a just created extension. As a test, I created a sample extension

extension String {
    func checkExtension() {

    }
}  

When I did that, neither did I save the changes in the Swift file, nor did I compile the code. But checkExtension was available for a String variable.

Query : How does SDK know without compilation, that an extension has been created and it's functions can be used in the corresponding class ?

Nitish
  • 13,845
  • 28
  • 135
  • 263

1 Answers1

0

Swift Front End Compiler takes care of that.

So Compiler is divided into Front end, and Back end.

Front End: Takes care of Lexical Analysis, Syntax analysis (parsing), Semantic analysis

Back End: Optimization, Machine Dependent optimizations, Code Generation. (sometimes analysis and optimization are called as middle end)

In Swift compiler-architecture, they are calling lexical analyser and Syntax analyser as Parser. which builds the AST(Abstract Syntax Tree). Which is used to know what belong where, and AST is used finding error when we type incorrectly and Auto Complete Etc. Simillarly swift extension works that way.

Backend comes into play when we build or Compiler our self. Which what we mostly refer to as compiler when we talk about compilers. But the fornt end is always in action in Most IDE's.

Reference : https://swift.org/compiler-stdlib/#compiler-architecture

Gokul G
  • 698
  • 4
  • 11