小编Lau*_*ent的帖子

在迭代闭包上下文时,我需要为自己分配一个变量来保持正确的值

如果没有i:= i,我得到的结果不正确(3,3,5,9,7,15).有了它,我得到(0,0,3,3,6,10),这是正确的.删除赋值类似于在循环结束时获取i的值.为什么?

package main

import "fmt"

type Handler interface {
    Handle(v int)
}

type Elem struct {
    Handler Handler
}

var elems []*Elem

type handlerFunc func(v int)

func (h handlerFunc) Handle(v int) { h(v) }

func main() {
    newElem := func(fn handlerFunc) {
        elem := &Elem{Handler: handlerFunc(fn)}
        elems = append(elems, elem)
    }

    for i := 0; i < 3; i++ {
        i := i // *** Why? ***
        newElem(func(v int) { fmt.Printf("%d, ", i+v) })
        newElem(func(v int) { fmt.Printf("%d, ", …
Run Code Online (Sandbox Code Playgroud)

go

1
推荐指数
1
解决办法
105
查看次数

Negative ArraySlice:索引超出范围

我无法弄清楚为什么我在循环中的第二次迭代中得到错误.你能帮我理解问题的来源吗?

let NumTracks = 3
let TrackBytes = 2

func readBytes(input: [UInt8]?) {
    if let input = input  {
        var input = input[0..<input.count]
        for _ in 0..<NumTracks {
            print(input[0..<TrackBytes]) // fatal error: Negative ArraySlice index is out of range
            input = input[TrackBytes..<input.count]
        }
    }
}
let samples = [UInt8]?([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
readBytes(samples)
Run Code Online (Sandbox Code Playgroud)

还有另外一个像这样的测试用例,没有理由说它也会崩溃.

编辑

当我使用此代码变体时,我没有收到错误(我仍然不知道为什么):

let NumTracks = 3
let TrackBytes = 2

func readBytes(input: [UInt8]?) {
    if let input = …
Run Code Online (Sandbox Code Playgroud)

swift

1
推荐指数
1
解决办法
893
查看次数

功能参数名称不符合文档

根据手册:

功能参数名称

这些参数名称仅在函数本体中使用,并且在调用函数时不能使用.这些参数名称称为本地参数名称,因为它们仅可在函数体内使用.

    join("hello", "world", ", ")


func join(s1: String, s2: String, joiner: String) -> String {
    return s1 + joiner + s2
}
Run Code Online (Sandbox Code Playgroud)

但是代码没有编译:

错误:缺少参数标签's2:joiner:'in call join("hello","world",",")^ s2:joiner:

只有当我尝试使用一个参数时,它才成为可选项

    join("hello")


func join(s1: String) -> String {
    return s1
}
Run Code Online (Sandbox Code Playgroud)

更令人讨厌的是,根本不允许使用第一个:

    join(s1: "hello")
Run Code Online (Sandbox Code Playgroud)

无关的参数标签's1:'在调用join(s1:"hello")中

在阅读涵盖功能的文档时,我是否遗漏了什么?

ios swift

0
推荐指数
1
解决办法
961
查看次数

在处理接口时,类型断言是返回结构指针的唯一方法吗?

考虑以下代码:

package main

import "fmt"
// 5
type I interface {
    Foo() string
}

type a struct {
    i int
}

func (s a) Foo() string {
    return "We are here!"
}

func (s a) Bar() string {
    return "Major Tom!"
}
// 20
func main() {
    var x I = &a{i: 42}
    fmt.Println(x.Foo())
    fmt.Println(x.(*a).Bar())
}
Run Code Online (Sandbox Code Playgroud)

main 的最后一个语句给了我底层结构,但我需要导出这个结构才能返回它。

如果我在库中使用一个包,其中导出的唯一符号是接口(在上面的示例中,第 5-20 行之间是大 I,小 a),那么当我处于另一个包或文件。

由于原始结构存储在接口中,是否有一种简单的方法来获取其引用并使用尚未在接口中声明且仅附加到结构的方法。

methods interface go type-assertion

0
推荐指数
1
解决办法
1823
查看次数

借用时暂时价值下降

我遇到这个问题,因为我正在解构的变量是借用的(?)并且不能在其他方法中使用。这听起来像是一个非常典型的用例,但我不确定如何解决它。

\n
`\xe2\x9e\x9c  hello_cargo git:(master) \xe2\x9c\x97 cargo build\n   Compiling hello_cargo v0.1.0 (/Users/johnny/Projects/hello_cargo)\nerror[E0716]: temporary value dropped while borrowed\n  --> src/main.rs:24:39\n   |\n24 |         let DBAndCFs { db: _, cfs } = self.db.lock().as_ref().unwrap();\n   |                                       ^^^^^^^^^^^^^^                  - temporary value is freed at the end of this statement\n   |                                       |\n   |                                       creates a temporary which is freed while still in use\n25 |         cfs.len()\n   |         --------- borrow later used here\n   |\n   = note: consider using a `let` binding to create a longer lived value\n`\n
Run Code Online (Sandbox Code Playgroud)\n

这是生成此问题的代码: …

rust

0
推荐指数
1
解决办法
6562
查看次数

标签 统计

go ×2

swift ×2

interface ×1

ios ×1

methods ×1

rust ×1

type-assertion ×1