草庐IT

swift - 内联 if 语句在 void 返回闭包中改变 inout 参数,奇怪的错误(错误 : type 'Int1' does not conform to protocol 'BooleanType' )

coder 2023-09-11 原文

我遇到了一个有点奇怪的(编译时)错误,我无法理解。以下片段给出了错误:

/* error: type 'Int1' does not conform to protocol 'BooleanType' */
let closure1 : (inout foo: Int) -> () = {
    foo -> () in
    (foo<0 ? (foo = -1) : (foo = 1))
}

Error: type 'Int1' does not conform to protocol 'BooleanType'

请注意,这里的 Int1 不是拼写错误。

问题 1:为什么我不允许使用单个内联 if 语句(结果为 '()')作为 void return 闭包的隐式返回类型?

问题 2: 出于好奇,Int1 类型是什么? (奇怪的是,即使修改上面的闭包以类似的方式对不同类型进行操作,也会给出相同的错误消息,Int1 ...)。

为什么我对此感兴趣?我有一些闭包,我想以类似于下面的 closure1Anon 的匿名形式使用,但由于这个错误我不能。

详细信息和我自己对此事的(非富有成果的)调查如下。


如上所述,在 void return 闭包中使用包含对 inout 参数赋值的单个内联 if 语句时会提示错误。

我们可以验证内联语句的结果是空元组(),考虑例如:

var foo = -4
print((foo<0 ? (foo = -1) : (foo = 1)).dynamicType) // ()
print(foo)                                          // -1

...所以它应该可以用作 void return 闭包的 return 语句(参见下面的赋值后跟返回示例 closure4)。

下面是错误的闭包(closure1closure1Explicitclosure1Anon)以及五个非常相似/相关但工作正常的闭包(closure2closure7)。

/* error: type 'Int1' does not conform to protocol 'BooleanType' */
let closure1 : (inout foo: Int) -> () = {
    foo -> () in
    (foo<0 ? (foo = -1) : (foo = 1))
}

/* same error */
let closure1Explicit : (inout foo: Int) -> () = {
    foo -> () in
    return (foo<0 ? (foo = -1) : (foo = 1))
}

let closure1Anon : (inout foo: Int) -> () = { ($0<0 ? ($0 = -1) : ($0 = 1)) }

好的:

/* The following are all OK */
let closure2 : (inout foo: Int) -> () = {
    (inout foo: Int) -> () in
    (foo<0 ? (foo = -1) : (foo = 1))
} // thanks @MartinR

let closure3 : (inout foo: Int) -> () = {
    foo -> () in
    let _ = (foo<3 ? (foo = 1) : (foo = 2))
}

let closure4 : (inout foo: Int) -> () = {
    foo -> () in
    (foo<3 ? (foo = 1) : (foo = 2))
    return ()
}

let closure5 : (inout foo: Int) -> () = {
    foo -> () in
    let bar = (foo<3 ? (foo = 1) : (foo = 2))
    return bar
}

/* Error must be related to inout as the two
   following closures works fine */
let closure6 : () -> () = {
    () -> () in
    var a = 0
    return (a<0 ? (a = -1) : (a = 1))
}

var globalVar = 1
let closure7 : () -> () = {
    () -> () in
    (globalVar<0 ? (globalVar = -1) : (globalVar = 1))
}

我无法弄清楚为什么上面的 closure1/closure1Explicit/closure1Anon 会产生此错误。可能有人可以为我阐明这一点?


最后说明:在这种情况下,以下看似相似的 SO 线程似乎不相关:


我使用的是 Swift 2.1.1 和 Xcode 7.2.1。

最佳答案

(添加这个简单的答案以结束不再相关的问题)

上述问题中描述的错误不再出现在 Swift 2.2 (Xcode 7.3) 中,也不再存在于 IBM Sandbox 的 Swift 3.0-dev 中。 ;所以这个问题似乎已经在 Swift 2.2 的发布中得到解决。

关于swift - 内联 if 语句在 void 返回闭包中改变 inout 参数,奇怪的错误(错误 : type 'Int1' does not conform to protocol 'BooleanType' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35184847/

有关swift - 内联 if 语句在 void 返回闭包中改变 inout 参数,奇怪的错误(错误 : type 'Int1' does not conform to protocol 'BooleanType' )的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  9. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

  10. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

随机推荐