Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build an Interactive Story App with Swift Refactoring Our Code Recap: Refactoring Our Code

Richard Lu
Richard Lu
20,185 Points

Unable to infer complex closure return type; add explicit type to disambiguate.

So the following code is equivalent and works without a problem

var someProperty = 1

//====================

var anotherProperty = { 
   return 1 
}()

Normally, there should be an explicit type - for example:

struct SomeStruct { }

var someStruct = {  // this gives an error
   let someStruct = SomeStruct()
   return someStruct
}()

The snippet of code above gives the error Unable to infer complex closure return type; add explicit type to disambiguate. To fix the issue, I added an explicit type:

struct SomeStruct { }

var someStruct: SomeStruct = {  // error goes away
   let someStruct = SomeStruct()
   return someStruct
}()

Why is this the case? Why does SomeStruct not behave the same as an Int (also a struct)?