【问题标题】:Chisel3: Vec indexWhere expected Bool, actual AnyChisel3: Vec indexWhere 预期 Bool,实际 Any
【发布时间】:2020-07-29 18:31:21
【问题描述】:

在 Chisel 中,我有一个 Bool 的 Vec 进入模块。我想知道发生的第一个 False 的索引。

为了获得这个,我尝试使用以下方法:

val faultIndex = Wire(UInt)

faultIndex := comparison.indexWhere(x:Bool => x === false.B)

当我把它放进去时,一个错误被突出显示:

Unspecified value parameters: from: Int
Type mismatch, expected Bool => Bool, actual: Bool => Any
Type mismatch, expected Bool => Boolean, actual: Bool => Any
Cannot resolve symbol x
Cannot resolve symbol x

使用此功能的正确方法是什么?

【问题讨论】:

    标签: chisel


    【解决方案1】:

    这里有 2 个小语法问题:

    val faultIndex = Wire(UInt())
    

    注意UInt 后面的()。您可以将其视为构造一个新类型对象,而不是指向名为 UInt 的静态对象。

    indexWhere有几种表达方式:

    faultIndex := comparison.indexWhere((x: Bool) => x === false.B) // Note parentheses
    // or
    faultIndex := comparison.indexWhere(x => x === false.B) // Type is inferred
    // or
    faultIndex := comparison.indexWhere(_ === false.B) // underscore shorthand
    // alternatively
    faultIndex := comparison.indexWhere(x => !x) // !x is equivalent to x === false.B
    // or
    faultIndex := comparison.indexWhere(!_)      // More underscore shorthand
    
    

    可执行示例:https://scastie.scala-lang.org/uHCX5wxgSzu6wXqa9OJdRA

    【讨论】:

    • 感谢您的洞察!将 Uint 更改为 UInt() 后,它变得更加清晰。如果您不介意,您能否解释一下 IndexWhere 函数的复杂输入的意义是什么?我不熟悉 C 以外的其他语言,所以我对这种编程技术并不熟悉。
    • indexWhere 是一个“高阶函数”(HOF),即。参数是一个函数本身(又名 lambda)。试图在 SO 评论中解释它们的实用性是一项艰巨的任务,所以我只建议阅读它们。虽然它们来自 C 有点陌生,但它们在现代编程语言中越来越普遍:例如。 Swift、Rust、Java、JavaScript、Python。
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多