0

I need to have a file input that can be executed as both - main for output, and a module for unit testing(because apparently you can't run tests again main.swift).

Let's say I have a file like this:

class Hello {}
#if RELEASE
  print("hello")
#endif

Now, the idea here is that I can put this input into either a main.swift or something.swift and use a flag to remove the print("hello") when not in main.swift.

But it seems that no matter what I still get the error:

error: expressions are not allowed at the top level print("hello")

Is it possible to achieve what I want with the flags, or there's something weird going?

Erndob
  • 2,512
  • 20
  • 32
  • @mag_zbc From my understanding, the build should completely ignore whatever is inside of the IF. That's the point of the preprocessor, no? Why do I get an error on code that shouldn't be part of the build itself. In main.swift file that print is supposed to get executed. In other files, through flags, it should behave as if it does not exist and not give a build error. – Erndob Jul 15 '20 at 11:39
  • @mag_zbc it executes in main.swift, it prints "Hello". That's what it does. – Erndob Jul 15 '20 at 11:51
  • Would you care to explain then what it does if I'm not understanding it correctly, instead of just saying "you are wrong". That's why I'm asking the question. – Erndob Jul 15 '20 at 11:52
  • Dude, you can. That's what an executable is. It literally allows you to put loose expressions inside of the main.swift files, and then you run with 'swift run PackageName'. – Erndob Jul 15 '20 at 11:56
  • https://stackoverflow.com/questions/24105690/what-is-the-entry-point-of-swift-code-execution – Erndob Jul 15 '20 at 11:59
  • 1. It doesn't work the same. In C# if I exclude a piece of code through preprocessor, that code doesn't get validated. It can have literally bad syntax that usually would fail immediately, but if it's excluded through flags, it's as if it doesn't exist. 2. Doing further tests with swift preprocessor, it doesn't work like that, try having invalid statement inside of a block that should be excluded, you still get an a compile error. – Erndob Jul 15 '20 at 12:19
  • 3. I'm not executing another file code inside a main file code thinking it allows me to use expressions. That's not what I asked. I have same input, executing it AS main.file or as lib/module file. And I wanted to exclude loose expressions from compilation through the processor, when I have the code not in the main file. – Erndob Jul 15 '20 at 12:22

0 Answers0