这种形式的作用域是怎么叫什么名字? [英] How is this form of scoping called?

查看:144
本文介绍了这种形式的作用域是怎么叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更多或更少的意外我偶然发现了这种形式的作用域

More or less by accident I stumbled upon this form of scoping

DataSource *dataSource =({
    NSInteger idx = [[self.tableView indexPathForSelectedRow] row];
    DataSource *dataSource = [DataSource new];
    dataSource.address = self.destinations[idx][0];
    dataSource.name    = self.destinations[idx][1];
    dataSource;
});

我觉得这是创建和实例化对象和变量一样需要它们来创建对象,我真的需要和照顾临时变量将仅在活的好方法。在code以上 IDX 将尽快为我写的内数据源外<$不见了C $ C>数据源,因为范围将很快后留下的。结果
此外,我觉得其实有吸引力,一个完全实例化和配置的对象将被设置为外部对象。结果
事实上,我甚至不知道这是否是一个C或Objective-C的功能或语法糖添加到铛。

I think it is a nice way to create and instantiate objects and variables as temporary variables will only live as long as they are needed to create the object I really need and care for. In the code above idx will be gone as soon as I write the inner dataSource to the outer dataSource, as the scope will be left soon after.
Also I find the fact appealing that a fully instantiated and configured object will be set to the outer object.
Actually I don't even know if this is a C or Objective-C feature or a syntax candy added to clang.

@Unheilig结果
这是组织code语法。它不是像块或关闭。在code月底你只是有一个完全实例化和配置的对象。

@Unheilig
this is a syntax for organizing code. it is not something like a block or closure. at the end of the code you just have a fully instantiated and configured object.

本就派上用场了,如果你需要一个对象只把它作为参数传递给方法,但是对象的配置需要多个语句。相反,它分配给本地临时变量,你可以在一份声明中前pression通过。

This comes in handy if you need an object only to pass it as an argument to a method, but the configuration of that object takes more than one statement. Instead of assigning it to a local temporary variable you can pass in a statement expression.

[[MYViewController alloc] initWithDataSource:({
    NSInteger idx = [[self.tableView indexPathForSelectedRow] row];
    DataSource *dataSource = [DataSource new];
    dataSource.address = self.destinations[idx][@"address"];
    dataSource.name    = self.destinations[idx][@"name"];
    dataSource;
})];

在非ARC环境中,你甚至可以除权pression语句内来电自动释放。

In a non-ARC environment you could even call autorelease inside the expression statement.

所以它只是code的组织和大量的个人口味,我猜。

So it is just about code organization and a lot of personal taste I guess.

推荐答案

这是一个GCC扩展,称为语句前pression,在<一个描述href=\"http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html\">http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html.

It is a GCC extension, called "statement expression", described at http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html.

这篇关于这种形式的作用域是怎么叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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