I think this is a great idea, but in its current form it looks to become a usability problem, in Xcode at least.
The problem is that, as soon as you enshrine even a relative path in code, something as simple as dragging a project item to a different place in the project hierarchy may easily break the code. A single, isolated instance of this is not hard to deal with, but in a large, source-controlled, multi-developer project, these kinds of errors build up to a constant nuisance.
A different problem arises when the file contains text. Traditionally, Xcode tolerates variations of encodings (UTF-8, UTF-16, and other things) and variations of line-endings in source code. Text files come from external sources with varying combinations of such variations, and by the time they're added to the project, the variations are opaque to text editing.
That says to me that such files need a "compilation" pass (in general). For pure binary data, the "compilation" is a pass-through. For text, the compilation is (say) a re-encoding to UTF-8 with standardized line ending. For other recognized types, if any, a different compilation might be needed.
Separately, it makes no sense to me that the contents of these files are literally compiled into other Swift source files. That make the compilation unit bigger and slower, and (by the syntax-coloring argument used as motivation) no one really wants to look at the file contents in situ in the source file.
Surely it would be better to break this entire proposal into two parts:
-
Compile the external file independently into an object file, with some mangled/namespaced public symbol, representing some kind of Sequence
-compatible data (e.g. a [UInt8]
if nothing better), that's linked into the final binary. Maybe this could use a double-barreled file extension convention such as .swiftdata.png
for binary, or .swifttext.sql
for readable text.
-
Change the #xxx
literal syntax to refer to the public symbol, using the associated data in an initializer for Data
, as currently proposed.
I realize this is not a trivial alternative, and ā more significantly ā cannot be done without changes to Xcode (at least to recognize the need to "compile" one of the special files). However, I can't think of any approach to this file-literal feature that can work reasonably without some Xcode support.
It would be unfortunate if this great feature was implemented in a way that caused Xcode users to shun it because it was too troublesome to use.
FWIW