zoecarver
(Zoe Carver)
1
Is there a way to import builtins created in the compiler? I know they can be used in the standard library but, I cannot find a way to use them in a test program. For example, how could I get access to Builtin.cmp_eq_Int64 or even Builtin.Int64 for that matter?
Alejandro
(Alejandro Alonso)
2
Theres no way to access these unless you’re the standard library. You really shouldn’t need to use these at all in user facing code which is why they aren’t allowed.
zoecarver
(Zoe Carver)
3
The reason I want to access them is to tests builtins I have created. Maybe just writing a test would be better, though.
Alejandro
(Alejandro Alonso)
4
If you’re testing a new Builtin, you can use the -parse-stdlib flag to allow usage of Builtins.
1 Like
zoecarver
(Zoe Carver)
5
Great. That is very helpful. Thank you!
zoecarver
(Zoe Carver)
6
@Alejandro unfortunately that did not work for me. I still get this error:
error: use of unresolved identifier 'Builtin'
I am invoking the program with:
$ build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift test/test.swift -parse-stdlib
Am I missing something? Thanks again for the help.
Alejandro
(Alejandro Alonso)
7
It works when I use swiftc, maybe try that?
zoecarver
(Zoe Carver)
8
Aha worked like a charm. Thanks!
1 Like
owenv
(Owen Voorhees)
9
I believe it's a frontend option, so -Xfrontend -parse-stdlib should also work
beccadax
(Becca Royal-Gordon)
10
When you run swift myscript.swift, arguments after the filename become arguments to the script, available in CommandLine.arguments, rather than arguments to Swift.
(Ask Me How I Know™.)
The other gotcha you'll probably run into: If you use -parse-stdlib but you still want access to the real standard library, you'll need to import Swift.
1 Like
owenv
(Owen Voorhees)
11
Thank you for pointing this out, I always forget that's the case! I filed SR-11304 to see if we can improve the UX in this area. It's difficult to guess whether an argument was intended for swift or the program being run in some cases, but for something like swift script.swift -parse-stdlib I think an "are you sure you didn't mean to pass this to swift" warning would be acceptable.
cukr
12
What if you want to pass -parse-stdlib to your script? In that case it will garble your output...