Nim
Nim copied to clipboard
Generic function does not match generic param to `void`
trafficstars
The compiler cannot match procedure types like proc (_: T): R.
See https://nim-lang.org/docs/manual.html#overload-resolution for the definition of "generic match"
Example
type
Callback*[S,R] = proc(_:S): R
State = object
proc foo*[S,R](action: Callback[S,R]) =
discard
proc noop(state: State): void = discard
foo(noop) # fail
foo[State,void](noop) # success
Current Output
Error: type mismatch: got <proc (state: State){.noSideEffect, gcsafe, locks: 0.}>
but expected one of:
proc foo[S, R](action: Callback[S, R])
first type mismatch at position: 1
required type for action: Callback[foo.S, foo.R]
but expression 'noop' is of type: proc (state: State){.noSideEffect, gcsafe, locks: 0.}
expression: foo(noop)
Expected Output
No compiler error
Possible Solution
idk
Additional Information
I was modifying someone's state machine library when I encountered this problem.
Offending part: addTransition does not match without explicit type parameters
import tables, options
type
Callback*[S,R] = proc(_:S): R
StateEvent*[S,E] = tuple[state: S, event: E]
Transition*[S,R] = tuple[nexState: S, action: Callback[S,R]]
Machine*[S,E,R] = object
state*: S
transitions*: Table[StateEvent[S,E], Transition[S,R]]
proc addTransition*[S,E,R](m: var Machine[S,E,R], state: S, event: E, nextState: S, action: Callback[S,R]) =
m.transitions[(state, event)] = (nextState, action)
Nim version
> nim -v
Nim Compiler Version 1.6.2 [Linux: amd64]
Compiled at 2021-12-17
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 9084d9bc02bcd983b81a4c76a05f27b9ce2707dd
active boot switches: -d:release