Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
user@user:~$ nim c --hints:off --gc:arc --run jj.nim
/home/user/jj.nim(19, 47) Error: type mismatch: got <Decree>
but expected one of:
template convertCommandToDecree[T; R; ](input: sink T): R
first type mismatch at position: 1
required type for input: Command
but expression 'Decree()' is of type: Decree
expression: convertCommandToDecree[Command1, Decree](Decree())
ERROR: sink example 3
The following code should not compile. Currently, it compiles but fails at runtime.
echo $convertCommandToDecree[Command1, Decree](Command2())
| |
| |
note this is still Command1 ^ |
^ but this is Command2
user@user:~$ nim c --hints:off --gc:arc --run jj.nim
/home/user/jj.nim(19) jj
/home/user/nim/lib/pure/json.nim(1301) to
/home/user/nim/lib/pure/json.nim(1197) initFromJson
/home/user/nim/lib/pure/json.nim(1013) initFromJson
Error: unhandled exception: key not found: .common [KeyError]
Error: execution of an external program failed: '/home/user/jj '
Current output without sink
If the parameter to the template is changed to be a non-sink parameter, like so:
template convertCommandToDecree[T: typedesc[Command], R](
input: T # no longer a sink parameter
): R =
parseJson(json.pretty(%input)).to(typedesc[R])
user@user:~$ nim c --hints:off --gc:arc --run jj.nim
/home/user/jj.nim(19, 47) Error: type mismatch: got <Command1>
but expected one of:
template convertCommandToDecree[T; R](input: T): R
first type mismatch at position: 1
required type for input: T
but expression 'Command1()' is of type: Command1
expression: convertCommandToDecree[Command1, Decree](Command1())
user@user:~$ nim c --hints:off --gc:arc --run jj.nim
/home/user/jj.nim(19, 47) Error: type mismatch: got <Decree>
but expected one of:
template convertCommandToDecree[T; R](input: T): R
first type mismatch at position: 1
required type for input: T
but expression 'Decree()' is of type: Decree
expression: convertCommandToDecree[Command1, Decree](Decree())
user@user:~$ nim c --hints:off --gc:arc --run jj.nim
/home/user/jj.nim(19, 47) Error: type mismatch: got <Command2>
but expected one of:
template convertCommandToDecree[T; R](input: T): R
first type mismatch at position: 1
required type for input: T
but expression 'Command2()' is of type: Command2
expression: convertCommandToDecree[Command1, Decree](Command2())
Summary
sink
non-sink
example 1
OK
error
example 2
OK
OK
example 3
error
OK
I don't know which behavior exactly is undesirable, but I think there is evidence of at least one bug here when all 6 examples are taken in unison.
Additional Information
Git hash 6c07b0a1f8daf96078ae68b83ead1d48675969d7
user@user:~$ nim --version
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-01-18
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 6c07b0a1f8daf96078ae68b83ead1d48675969d7
active boot switches: -d:release
When a parameter to a template is a
sink
parameter, the compiler does not compile appropriately.Setup
This example is written with the following in mind:
Decree
because of how the template is invokedCommand
s should be allowed forinput
, namely eitherCommand1
orCommand2
Decree
andCommand1
share a variable name and type in common, whileDecree
andCommand2
do notCurrent output with
sink
OK:
sink
example 1The following code should compile and runs successfully. Currently, it does compile and run.
OK:
sink
example 2The following code should not compile. Currently, it does not compile.
ERROR:
sink
example 3The following code should not compile. Currently, it compiles but fails at runtime.
Current output without
sink
If the parameter to the template is changed to be a non-
sink
parameter, like so:then none of examples 1, 2, or 3 compile.
ERROR: non-
sink
example 1OK: non-
sink
example 2OK: non-
sink
example 3Summary
sink
sink
I don't know which behavior exactly is undesirable, but I think there is evidence of at least one bug here when all 6 examples are taken in unison.
Additional Information
Git hash 6c07b0a1f8daf96078ae68b83ead1d48675969d7