Types of security and type inference such as Swift

Source: Internet
Author: User

Swift is a type-safe language. The type-safe language encourages the recommended values in your code to be as clear as possible about the type that they belong to. If you have a pre-good string in your code, you cannot mistakenly pass or assign it to a whole number.

Because Swift is type-safe, it does type detection when compiling your code and identifies all mismatched types in an incorrect manner. This allows you to capture early positioning errors during the development process.

Type detection can help you avoid errors when you work between different types, which doesn't mean you have to specify a definite type for each declared constant and variable. If you do not specify a type for the value you need, Swift uses type inference to calculate its appropriate type. Type inference allows the compiler to automatically infer the type of a particular representation when compiling your code. Can easily detect the value you provide.

Because of type inference, Swift needs to define fewer types than other languages (C or objective-c). Constants and variables are also types that you want to be explicit, but you do not need to specify their type of work.

Type inference is particularly useful when declaring constants or variables through an initial value. When you create a constant or a variable, you often assign a literal value to them (the literal value appears directly in your code, such as 42,3,14159 in the example below).

For example, if you are a new constant and you do not specify a reconciled type by assigning a literal value of 42, Swift can infer that the type of the constant you want is an int, because you initialized its value with an integer:

Let Meaningoflift = 42

Type inference infers that its type is int

Similarly, you do not need to specify the type as a literal floating-point number, and swift can infer that you want to create a double

Let pi = 3,14159

The type of pi is double

Swift always chooses double when it infers a floating-point data type.

If you add an integer to the literal of a floating-point data in an expression, the type inference will be inferred by the context of the type double:

Let Anotherpi = 3 + 0.14159

The type of Anotherpi is also double

3 its own literal value does not have a definite type (display type), so the type of the output double is inferred from the literal value of the existing floating-point number portion.

Types of security and type inference such as Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.