I added a "safe(int): int" proc to the above example (for convenience):
    
    
    # nim js -d:release -d:nodejs --verbosity:0
    
    type Foo = ref object
      a: cstring
      b: int
    
    proc isUndefined*[T](x: T): bool {.importcpp: "((#)==undefined)".}
    proc safe*(x: int): int {.importcpp: "((#)||0)".}
    
    proc main() =
      var o {.noinit.}: Foo
      {.emit: "var `o` = {};".}
      o.a = "test"
    
    #[ this is Nim not javascript
      if o.b == nil:
        echo "o.b is not is nil"
    ]#
      
      if o.b.isUndefined:
        echo "but then we are Nim and Nim is King"
      
      echo o.b.safe # 0
      
      o.b = 3
      
      echo o.b.safe # 3
    
    main()
    

Reply via email to