7
\$\begingroup\$

This is a challenge for the people using Swift 2.0 beta 6, it's not code golf, but it's a programming puzzle so I'm pretty sure this belongs here. The basic idea is: Make it compile. There are more details and a very nicely formatted playground at the Github project. Previous challenges are there as well, all set up to tinker. Solutions are included for the previous challenges, as they all have been solved.

Rules

  • You can't modify the given lines
  • You can't insert anything between the given lines
  • You can't provide custom functions for ones that are in the standard library, according to the syntax-hightlighed picture. This applies to the inverse as well.
  • The given lines are not allowed to produce a warning (on default warning settings). However the code you append is allowed to produce warnings.
  • The code may or may not crash at runtime, it only has to compile

The Code

func doIt(inout s: D) {
    if case self = isUniquelyReferenced(&s),
        self.`self` = true where self {
        self.`self` = { [unowned x = [self, 7] as Self] n in
            print(x + n)
        } as () -> ()
    }
}

The Picture

Winner

The first one posting an answer that compiles and conforms to all the rules above wins and gets an entry in the projects Hall of Fame.

EDIT: Hint

Upon submission of Chris Goldsby's 99% solution, he received the following hint:

The syntax highlighting of

protocol P {
    typealias P
    var p : P { get }
}
\$\endgroup\$
  • 3
    \$\begingroup\$ Instead of just saying "latest swift beta version", could you specify which version that is, so that updates to swift don't break anything? \$\endgroup\$ – isaacg Sep 3 '15 at 7:50
  • \$\begingroup\$ @isaacg Changed it, thanks :) \$\endgroup\$ – Kametrixom Sep 3 '15 at 7:55
  • 1
    \$\begingroup\$ What's to stop me from commenting out all the lines? \$\endgroup\$ – feersum Sep 3 '15 at 8:36
  • \$\begingroup\$ @feersum The third rule, picture has to match \$\endgroup\$ – Kametrixom Sep 3 '15 at 8:37
  • \$\begingroup\$ I believe this could be edited to fix it... but I don't know Swift, so I can't do it. Future reviewers , please try. \$\endgroup\$ – wizzwizz4 Mar 13 '16 at 8:22
2
\$\begingroup\$

I'm posting this answer on behalf of Chris Goldsby who managed to submit a

99% Solution (Gist):

typealias D = NonObjectiveCBase

func ~=(left: Any, right: Bool) -> BooleanType {
    return true
}

func +(left: Any, right: Any) -> String {
    return ""
}

protocol 🐢: class, BooleanType, ArrayLiteralConvertible {

    var `self` : () -> () { get set }
}

extension 🐢 {

    typealias Element = Any

    internal init(arrayLiteral elements: Any...) {
        self.init()
    }

    //

    func doIt(inout s: D) {
        if case self = isUniquelyReferenced(&s),
            self.`self` = true where self {
            self.`self` = { [unowned x = [self, 7] as Self] n in
                print(x + n)
            } as () -> ()
        }
    }

    //

}

Only the snytax highlighting of D doesn't match. That's remarkable as it is!

100% Solution

EDIT: Two days later he submitted the following full solution, congrats!:

private func ~=(left: Any, right: Bool) -> BooleanType {
    return true
}

private func +(left: Any, right: Any) -> String {
    return ""
}

protocol Challenge4: class, BooleanType, ArrayLiteralConvertible {

    typealias D

    var `self` : () -> () { get set }

    func doIt(inout s: D)
}

private extension Challenge4 where D == NonObjectiveCBase {

    typealias Element = Any

    init(arrayLiteral elements: Any...) {
        self.init()
    }

    //

    func doIt(inout s: D) {
        if case self = isUniquelyReferenced(&s),
            self.`self` = true where self {
            self.`self` = { [unowned x = [self, 7] as Self] n in
                print(x + n)
            } as () -> ()
        }
    }

    //

}
|improve this answer|||||
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.