Write Your First Generic Function

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write firstOrDefault(items, fallback): return the first item of the list, or fallback when the list is empty. Make it generic, so that it works for a list of any type and its result has that type: a list of strings gives a string. A fallback of a different type than the items should be a type error.

Your code must pass

  • Returns the first item
  • Returns the fallback for an empty list
  • Has no type errors
  • The result has the items' type
  • Rejects a fallback of another type