Failable initialisers?

I couldn’t get either form of failable inits to compile in build .1917, are they supposed to be implemented?

This code compiles in XCode:


var str = "Hello, playground"

class MyError: ErrorType
{}

class MyClass
{
   // compile error on the ?
    init?()
    {
        return nil
    }
    
   // compile error on the throws
    init(secondInit: String) throws
    {
        throw MyError()
    }
    
}

let mc = MyClass()

Attached project

SilverTest6.zip (139.6 KB)

They should be, yes.

Thanks, logged as bugs://74293

cool. Do you think there will be a timely fix? I hate bugging on bug reports, but I need to work out whether to do a ton of refactoring to remove throws from about 15 odd constructors and change them to static factory constructors, or wait for a new Silver build

I’ll talk to the team to see if we can give it prio. Won’t make it into tomorrow’s build though, sorry. Next week should be feasible (but no promises).

Thanks

1 Like

Just checking all my issues - still fails in .1947

Just ran into this. I understand you guys have to establish/follow priorities, but it might be worth putting in the time to create a more comprehensive “Limitations” document to describe the subset of Swift you support. Although I think you’ve done a great job in terms of scope, I think the current document gives an unrealistic impression.

bugs://74293 got closed with status fixed.

As of build 10.0.0.2309 the following code with a failable initializer, is not failing, when it should:

struct employeeStruct {
  var firstName="John"
  var lastName="Doe"
  var salary=30000.0

  init?(fn:String,ln:String, sy:Double) { 
    firstName=fn; lastName=ln; salary=sy
    if sy<20000 { return nil }
  }
}

var es=employeeStruct(fn:"Jon",ln:"Hatol",sy:10000)
print(es==nil)

es is getting initialized.

Thanks, logged as bugs://80630

bugs://80630 got closed with status fixed.