Allow to omit type when default value is present

This commit is contained in:
Ruslan Mustakov 2017-08-06 13:43:11 +07:00
commit 72b706d7fe
2 changed files with 15 additions and 5 deletions

View file

@ -119,6 +119,12 @@ proc identDefsToVarDecls(identDefs: NimNode): seq[VarDecl] =
assert(identDefs.kind == nnkIdentDefs) assert(identDefs.kind == nnkIdentDefs)
result = newSeqOfCap[VarDecl](identDefs.len - 2) 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): for i in 0..<(identDefs.len - 2):
let nameNode = identDefs[i].copyNimTree() let nameNode = identDefs[i].copyNimTree()
let hint = removeStrPragma(nameNode, "hint") let hint = removeStrPragma(nameNode, "hint")
@ -129,7 +135,7 @@ proc identDefsToVarDecls(identDefs: NimNode): seq[VarDecl] =
result.add(VarDecl( result.add(VarDecl(
name: if nameNode.kind == nnkPragmaExpr: nameNode[0].basename() name: if nameNode.kind == nnkPragmaExpr: nameNode[0].basename()
else: nameNode.basename(), else: nameNode.basename(),
typ: identDefs[identDefs.len - 2], typ: typ,
defaultValue: identDefs[identDefs.len - 1], defaultValue: identDefs[identDefs.len - 1],
hint: hint, hint: hint,
hintStr: hintStr, hintStr: hintStr,
@ -273,13 +279,13 @@ macro invokeVarArgs(procIdent, objIdent;
proc typeError(nimType: string, value: string, godotType: VariantType, proc typeError(nimType: string, value: string, godotType: VariantType,
className: cstring, propertyName: cstring): string = className: cstring, propertyName: cstring): string =
result = "Tried to assign incompatible value " & value & " (" & $godotType & result = "Tried to assign incompatible value " & value & " (" & $godotType &
") to field \"" & $propertyName & "\" (" & $nimType & ") of " & ") to field \"" & $propertyName & ": " & $nimType & "\" of " &
$className $className
proc rangeError(nimType: string, value: string, className: cstring, proc rangeError(nimType: string, value: string, className: cstring,
propertyName: cstring): string = propertyName: cstring): string =
result = "Tried to assign the out-of-range value " & value & result = "Tried to assign the out-of-range value " & value &
" to field \"" & $propertyName & "\" (" & $nimType & ") of " & " to field \"" & $propertyName & ": " & $nimType & "\" of " &
$className $className
@ -637,5 +643,7 @@ macro gdobj*(definition: untyped, body: typed): typed {.immediate.} =
## that you can find in `Godot API <index.html#modules-godot-api>`_. ## that you can find in `Godot API <index.html#modules-godot-api>`_.
let typeDef = parseType(definition, callsite()) let typeDef = parseType(definition, callsite())
result = genType(typeDef) result = genType(typeDef)
if typeDef.name == "MyObj":
echo repr result
{.push warning[Deprecated]: on.} {.push warning[Deprecated]: on.}

View file

@ -664,11 +664,13 @@ proc fromVariant*[T: array](s: var T, val: Variant): ConversionResult =
let arr = val.asArray() let arr = val.asArray()
if s.len != arr.len: if s.len != arr.len:
return ConversionResult.TypeError 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 mixin fromVariant
let convResult = fromVariant(s[idx], item) let convResult = fromVariant(s[nimIdx], item)
if convResult != ConversionResult.OK: if convResult != ConversionResult.OK:
return convResult return convResult
inc nimIdx
else: else:
result = ConversionResult.TypeError result = ConversionResult.TypeError