The Swift Make-It-Compile Challenge #4

7

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 }
}

Kametrixom

Posted 2015-09-03T07:38:20.030

Reputation: 426

Question was closed 2015-09-17T23:42:02.703

3Instead of just saying "latest swift beta version", could you specify which version that is, so that updates to swift don't break anything? – isaacg – 2015-09-03T07:50:15.297

@isaacg Changed it, thanks :) – Kametrixom – 2015-09-03T07:55:30.930

1What's to stop me from commenting out all the lines? – feersum – 2015-09-03T08:36:05.557

@feersum The third rule, picture has to match – Kametrixom – 2015-09-03T08:37:29.850

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. – wizzwizz4 – 2016-03-13T08:22:05.520

Answers

2

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 () -> ()
        }
    }

    //

}

Kametrixom

Posted 2015-09-03T07:38:20.030

Reputation: 426