zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【ASP.NET 问题】IIS发布网站后出现 "处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误"的解决办法
    在引用阿里云库或其他库的时候,经常发生框架不兼容(原因是系统采用:Microsoft .NET Framework 4 Client Profile ),请改为Microsoft .NET Framework 4
    jquery之cookie操作
    Kubernetes Pod 镜像拉取策略
    Kubernetes 远程工具连接k8s集群
    Kubernetes 部署Web UI (Dashboard)
    Kubernetes 企业级集群部署方式
    Prometheus 运维监控
    Prometheus 编写告警规则案例
    Prometheus 一条告警的触发流程、等待时间
  • 原文地址:https://www.cnblogs.com/feng9exe/p/9102835.html
Copyright © 2011-2022 走看看