I made a modification to the types to use a seq instead of a table:
import playdate/api
import std/json
import std/options
import std/sequtils
import utils
import shared_types
let kFileReadAny*: FileOptions = cast[FileOptions]({kFileRead,
kFileReadData})
type
LevelProgressEntity* = ref object of RootObj
levelId*: Path
bestTime*: Option[Seconds]
hasCollectedStar*: bool
SaveSlotEntity* = ref object of RootObj
progress*: seq[LevelProgressEntity]
modelVersion*: int
proc saveJson*[T](value: T, path: string) {.raises:[].} =
let jsonNode = %(value)
[...] more code
Run
Gives this error:
Executing /Users/ninovanhooff/.nimble/bin/nim c --colors:on
--noNimblePath -d:simulator -d:debug -d:NimblePkgVersion=0.2.0
--path:'/Users/ninovanhooff/.nimble/pkgs/playdate-#main'
--path:/Users/ninovanhooff/.nimble/pkgs/chipmunk7-7.0.3
-o:/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/wheelsprung
/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/src/wheelsprung.nim
Hint: used config file
'/Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/config/nim.cfg' [Conf]
Hint: used config file
'/Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/config/config.nims' [Conf]
Hint: used config file
'/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/config.nims'
[Conf]
.......................................................................................................................
/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/src/common/json_utils.nim(4,
11) Warning: imported and not used: 'sequtils' [UnusedImport]
.
/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/src/data_store/user_profile.nim(76,
11) template/generic instantiation of `saveJson` from here
/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/src/common/json_utils.nim(23,
18) template/generic instantiation of `%` from here
/Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/pure/json.nim(404,
14) template/generic instantiation of `%` from here
/Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/pure/json.nim(397,
41) template/generic instantiation of `%` from here
/Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/pure/json.nim(364,
36) Error: type mismatch: got <LevelProgressEntity>
but expected one of:
func `%`(formatstr, a: string): string
first type mismatch at position: 1
required type for formatstr: string
but expression 'elem' is of type: LevelProgressEntity
func `%`(formatstr: string; a: openArray[string]): string
first type mismatch at position: 1
required type for formatstr: string
but expression 'elem' is of type: LevelProgressEntity
proc `%`(b: bool): JsonNode
first type mismatch at position: 1
required type for b: bool
but expression 'elem' is of type: LevelProgressEntity
proc `%`(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode
first type mismatch at position: 1
required type for keyVals: openArray[tuple[key: string, val: JsonNode]]
but expression 'elem' is of type: LevelProgressEntity
proc `%`(n: BiggestInt): JsonNode
first type mismatch at position: 1
required type for n: BiggestInt
but expression 'elem' is of type: LevelProgressEntity
proc `%`(n: BiggestUInt): JsonNode
first type mismatch at position: 1
required type for n: BiggestUInt
but expression 'elem' is of type: LevelProgressEntity
proc `%`(n: float): JsonNode
first type mismatch at position: 1
required type for n: float
but expression 'elem' is of type: LevelProgressEntity
proc `%`(n: int): JsonNode
first type mismatch at position: 1
required type for n: int
but expression 'elem' is of type: LevelProgressEntity
proc `%`(n: uint): JsonNode
first type mismatch at position: 1
required type for n: uint
but expression 'elem' is of type: LevelProgressEntity
proc `%`(s: string): JsonNode
first type mismatch at position: 1
required type for s: string
but expression 'elem' is of type: LevelProgressEntity
proc `%`[T](elements: openArray[T]): JsonNode
first type mismatch at position: 1
required type for elements: openArray[T]
but expression 'elem' is of type: LevelProgressEntity
template `%`(j: JsonNode): JsonNode
first type mismatch at position: 1
required type for j: JsonNode
but expression 'elem' is of type: LevelProgressEntity
expression: %elem
Error: Build failed for package: wheelsprung
Run
Note that
* sequtils is unused
* when I change the last line to
let jsonNode = %(SaveSlotEntity())
Run
It works fine. So is there anything regarding generics I'm doing wrong?