Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)?