From 72b706d7fea4a7381e5fe52eb3c9363f096cde50 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Sun, 6 Aug 2017 13:43:11 +0700 Subject: [PATCH] Allow to omit type when default value is present --- godot/nim/godotmacros.nim | 14 +++++++++++--- godot/nim/godotnim.nim | 6 ++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/godot/nim/godotmacros.nim b/godot/nim/godotmacros.nim index 9574b4c..32e861d 100644 --- a/godot/nim/godotmacros.nim +++ b/godot/nim/godotmacros.nim @@ -119,6 +119,12 @@ proc identDefsToVarDecls(identDefs: NimNode): seq[VarDecl] = assert(identDefs.kind == nnkIdentDefs) result = newSeqOfCap[VarDecl](identDefs.len - 2) + var typ = identDefs[identDefs.len - 2] + if typ.kind == nnkEmpty: + let defaultValue = identDefs[identDefs.len - 1] + if defaultValue.kind != nnkEmpty: + typ = newCall("type", defaultValue) + for i in 0..<(identDefs.len - 2): let nameNode = identDefs[i].copyNimTree() let hint = removeStrPragma(nameNode, "hint") @@ -129,7 +135,7 @@ proc identDefsToVarDecls(identDefs: NimNode): seq[VarDecl] = result.add(VarDecl( name: if nameNode.kind == nnkPragmaExpr: nameNode[0].basename() else: nameNode.basename(), - typ: identDefs[identDefs.len - 2], + typ: typ, defaultValue: identDefs[identDefs.len - 1], hint: hint, hintStr: hintStr, @@ -273,13 +279,13 @@ macro invokeVarArgs(procIdent, objIdent; proc typeError(nimType: string, value: string, godotType: VariantType, className: cstring, propertyName: cstring): string = result = "Tried to assign incompatible value " & value & " (" & $godotType & - ") to field \"" & $propertyName & "\" (" & $nimType & ") of " & + ") to field \"" & $propertyName & ": " & $nimType & "\" of " & $className proc rangeError(nimType: string, value: string, className: cstring, propertyName: cstring): string = result = "Tried to assign the out-of-range value " & value & - " to field \"" & $propertyName & "\" (" & $nimType & ") of " & + " to field \"" & $propertyName & ": " & $nimType & "\" of " & $className @@ -637,5 +643,7 @@ macro gdobj*(definition: untyped, body: typed): typed {.immediate.} = ## that you can find in `Godot API `_. let typeDef = parseType(definition, callsite()) result = genType(typeDef) + if typeDef.name == "MyObj": + echo repr result {.push warning[Deprecated]: on.} diff --git a/godot/nim/godotnim.nim b/godot/nim/godotnim.nim index bab3dd1..c199216 100644 --- a/godot/nim/godotnim.nim +++ b/godot/nim/godotnim.nim @@ -664,11 +664,13 @@ proc fromVariant*[T: array](s: var T, val: Variant): ConversionResult = let arr = val.asArray() if s.len != arr.len: return ConversionResult.TypeError - for idx, item in arr: + var nimIdx = low(s) # may not start from 0 and may even be an enum + for item in arr: mixin fromVariant - let convResult = fromVariant(s[idx], item) + let convResult = fromVariant(s[nimIdx], item) if convResult != ConversionResult.OK: return convResult + inc nimIdx else: result = ConversionResult.TypeError