Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Types 尼姆:“是的。”;无效类型。。。在这种情况下,“;错误_Types_Compiler Errors_Nim Lang - Fatal编程技术网

Types 尼姆:“是的。”;无效类型。。。在这种情况下,“;错误

Types 尼姆:“是的。”;无效类型。。。在这种情况下,“;错误,types,compiler-errors,nim-lang,Types,Compiler Errors,Nim Lang,我想用尼姆和埃尔姆。Elm有一个端口系统,可以发送和接收消息,类似于web workers 我在主模块中有以下代码: import ./elm import ./portMessages type State = object elmProgram: elm.Program portToElm: elm.SubPort[portMessages.Outgoing] # Error is with this function proc newStat

我想用尼姆和埃尔姆。Elm有一个端口系统,可以发送和接收消息,类似于web workers

我在主模块中有以下代码:

import ./elm
import ./portMessages


type
    State = object
        elmProgram: elm.Program
        portToElm: elm.SubPort[portMessages.Outgoing]


# Error is with this function
proc newState(
    elmProgram: elm.Program,
): State =
    return State(
        elmProgram: elmProgram,
        portToElm: elmProgram.getSubPort("rawSubscribe"),
    )
当我尝试编译时,我从编译器获得以下错误消息:

Error: invalid type: 'SubPort[SubPort.T]' in this context: 'proc (elmProgram: Program): State' for proc
这是
elm
模块。
子端口
类型允许通过端口发送
T

type
    Program* {.importjs.} = ref object of JsRoot
    SubPort*[T: JsRoot] {.importjs.} = ref object of JsRoot


# Get a reference to a Sub port (to Elm)
proc getSubPort*[T: JsRoot](
    program: Program,
    name: cstring,
): SubPort[T] {.importjs: "#.ports[#]".}
这是“portMessages”模块:

type
    OutgoingKind* {.importjs.} = enum
        portErrorPopup = 0,
    
    Outgoing* {.importjs.} = ref object of JsRoot
        case kind*: OutgoingKind
        of portErrorPopup:
            msg*: cstring

类型不匹配是因为
getSubPort(“messages”)
仍然是一个未指定的泛型,因为它的返回类型是
SubPort[T]
,不能从参数中推断出来

同时,您已经指定您的
状态
类型的成员
portToElm
具有类型
子端口[Outgoing]

答案应该简单到:

proc newState(elmProgram: Program): State =
    State(
        elmProgram: elmProgram,
        portToElm: getSubPort[Outgoing](elmProgram,"rawSubscribe")
    )
但由于编译器错误,这仍然不起作用。解决方法是使用通用限制的概念:

elm
中:

type
  Program* {.importjs.} = ref object of JsRoot
  IsJsRoot = concept type t
    t is JsRoot
  SubPort*[T: IsJsRoot] {.importjs.} = ref object of JsRoot

proc getSubPort*[T:IsJsRoot](
    program: Program, name: cstring): SubPort[T]{.importjs: "#.ports[#]".}

(我不明白为什么子端口首先需要是泛型的,特别是因为它是importjs,但这不是重点)

子端口的定义中删除
:JsRoot
类型:

type
    # ...
    SubPort*[T] {.importjs.} = ref object of JsRoot
调用
getSubPort
时,还需要指定
portMessages.Outgoing

return State(
    # ...
    portToElm: elmProgram.getSubPort[: portMessages.Outgoing]("rawSubscribe"),
)

我尝试了这个,但它仍然给出了相同的错误`` proc portToElm(state:state,):elm.SubPort[portMessages.Outgoing]=return elm.getSubPort[portMessages.Outgoing](state.elmProgram,“rawSubscribe”)``这绝对不是
portToElm
主体的问题。我用
raisenewexception(…)
替换了它,错误与此完全相同。你是对的,我认为这是一个错误,用一个变通方法编辑了我的答案