Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's worth nothing that Swift literals are mostly library constructs as well. Any type conforming to ArrayLiteralConvertible can be instantiated using [...] syntax, for example. All other literal types (integers, floats, strings, dictionaries, etc.) have their own corresponding protocols you can implement. The only thing that's special for types like Array, Int, or String is that the compiler will automatically infer those types for a literal if there's nothing that indicates otherwise.

For example:

  let x = [1, 2, 3] // produces Array
  let y: MyType = [1, 2, 3] // produces MyType
  f([1, 2, 3]) // produces whatever type f() takes



Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: