单元素括号表达式/元组与括号的常见用法 [英] Single-element parethesized expressions/tuples vs common use of parentheses

查看:26
本文介绍了单元素括号表达式/元组与括号的常见用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是微不足道的 - 我是 swift 的新手,实际上我只看了几分钟的语言指南+参考.

Sorry if this is trivial - I am so new to swift, actually I have only looked at the language guide+reference for a few minutes.

据我所知,像 (2,3) 这样的括号表达式用于构造元组,而 (2) 是类型为单元素的元组(Int).

As far as I understand a parenthesized expression like (2,3) is used to construct a tuple, and (2) is a single-element tuple of type (Int).

但是,在表达式 (2+4)*5 中通常使用诸如 (2+4) 之类的括号会发生什么?这仍然是 (Int) 类型的元组乘以 Int 吗?

But then what happens with common use of parentheses like (2+4) in expression (2+4)*5? Is this still a tuple of type (Int) multiplied by an Int?

推荐答案

来自 :

From Types in the Swift book:

如果括号内只有一个元素,则类型为只是该元素的类型.例如,(Int) 的类型是Int,不是(Int).

If there is only one element inside the parentheses, the type is simply the type of that element. For example, the type of (Int) is Int, not (Int).

所以(2)(2+4)的类型就是Int*> 在 (2+4)*5 中是只是整数乘法.

So the type of (2) or (2+4) is simply Int, and the * in (2+4)*5 is just integer multiplication.

这篇关于单元素括号表达式/元组与括号的常见用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆