• Type Safety and Type Inference


    Swift is a type-safe language. A type safe language encourages you to be clear about the types of values your code can work with. If part of your code requires a String, you can’t pass it an Int by mistake.

    Because Swift is type safe, it performs type checks when compiling your code and flags any mismatched types as errors. This enables you to catch and fix errors as early as possible in the development process.

    Type-checking helps you avoid errors when you’re working with different types of values. However, this doesn’t mean that you have to specify the type of every constant and variable that you declare. If you don’t specify the type of value you need, Swift uses type inference to work out the appropriate type. Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide.

    Because of type inference, Swift requires far fewer type declarations than languages such as C or Objective-C. Constants and variables are still explicitly typed, but much of the work of specifying their type is done for you.

    Type inference is particularly useful when you declare a constant or variable with an initial value. This is often done by assigning a literal value (or literal) to the constant or variable at the point that you declare it. (A literal value is a value that appears directly in your source code, such as 42 and 3.14159 in the examples below.)

    For example, if you assign a literal value of 42 to a new constant without saying what type it is, Swift infers that you want the constant to be an Int, because you have initialized it with a number that looks like an integer:

    1. let meaningOfLife = 42
    2. // meaningOfLife is inferred to be of type Int

    Likewise, if you don’t specify a type for a floating-point literal, Swift infers that you want to create a Double:

    1. let pi = 3.14159
    2. // pi is inferred to be of type Double

    Swift always chooses Double (rather than Float) when inferring the type of floating-point numbers.

    If you combine integer and floating-point literals in an expression, a type of Double will be inferred from the context:

    1. let anotherPi = 3 + 0.14159
    2. // anotherPi is also inferred to be of type Double

    The literal value of 3 has no explicit type in and of itself, and so an appropriate output type of Double is inferred from the presence of a floating-point literal as part of the addition.

    https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID322

  • 相关阅读:
    python文件读取
    python字符串的使用
    需求验证实现
    面向对象思想——面向对象设计原则
    java面试问题整理-垃圾回收
    Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector
    Java集合类源码学习- Iterabel<T>,Colection<E>,AbstractCollection<E>
    EMF学习,为了实现可扩展可自定义的模型验证
    EMF学习,为了实现可扩展可自定义的模型验证
    Google 云计算中的 GFS 体系结构
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9102835.html
Copyright © 2020-2023  润新知