2
\$\begingroup\$

This is a very strange hack that I came across the other day. In the Swift REPL, if I create an instance of any type (say for example, a String), it automatically gets assigned to a variable in the same way an argument to a closure is. The name used is $R{n}. Here is an example:

enter image description here

However, this only works in the REPL. So I can I use this hack in a Swift answer if I specify that you have to run it like this:

cat file.swift | swift

Note that you do get extra output in the form of the blue output in the REPL.

\$\endgroup\$
3
  • 6
    \$\begingroup\$ This belongs on our meta site, and I have voted to migrate. \$\endgroup\$
    – Okx
    Aug 25, 2017 at 16:12
  • \$\begingroup\$ I think yes, because Mathematica REPL is valid. (although that is (probably) the default way to run MMA). MMA "last output" is even shorter, %. \$\endgroup\$
    – DELETE_ME
    Aug 25, 2017 at 16:14
  • \$\begingroup\$ You can, if your language is "Swift REPL", not "Swift". Also, might want to migrate this to meta. \$\endgroup\$
    – Mr. Xcoder
    Aug 25, 2017 at 16:17

1 Answer 1

9
\$\begingroup\$

Yes, but Swift and Swift REPL are different.

Yes, you are allowed to use all the features of REPL Swift (Terminal Swift), as long as you specify that your language is Swift REPL, not Swift. For example, your header should not look like this:

#Swift, X bytes

Instead, it should be:

#Swift REPL, X bytes

The same applies to all languages with a REPL version, such as Python and Python REPL.


If you want to keep it in Swift, you may use your code from the REPL version, and specify that your program must be ran with:

cat file.swift | swift

If you include cat file.swift | swift in your byte count (i.e. <program bytes> + X bytes (for cat file.swift | swift))

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .