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 Generics in Swift Generic Functions, Parameters and Constraints Generic Functions with Constraints

swift_lint.swift:5:20: error: expected initial value after '=' let result = in.max {$0 < $1}

any idea why it doesn't work

generics.swift
func largest<T:Comparable>(in:[T]) -> T? {

      let result = in.max  {$0 < $1}


  return result

}

3 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,861 Points

Hi John,

I just took a look at the challenge and it is specifically asking the the external name to be in. I still think that's not a great choice of name, but at least you don't have to use it inside the function body (you use the internal name inside the body).

Most of the reserved words are allowed in function declarations and calls but not allowed in the function body. Using in as an external name only gets around that issue.

If you were really dead set on using a reserved word in a place where it is prohibited, Apple lets you use it if you escape it with backticks.

Cheers

Alex

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,861 Points

Hi John,

The clue is in the colour of the word in in your code. Note that it is the same colour as the keywords Comparable and return and same colour as the constant name result. This indicates that in is a reserved keyword in Swift.

Sure enough, a look at the language guide reveals that in is a reserved keyword.

Try changing your in to something else, even inp and your code should work as expected.

Cheers

Alex

thks for your answer unfortunately if i'm trying to change the word the compiler is asking me to keep the reserved word. An other funny challenge from treehouse?