I just finished the new chapter and wanted to share some feedback.

1\. In 8.1.3 Type compatibility, you point out that we can't use int and string 
and should use cint and cstring instead. But instead of explaining the 
differences, you state that cstring is actually similar to string (to the point 
where string is implicitly converted to cstring) and cint is basically int32. 
If the types are in fact so similar, why can't we use them?

2\. `printf` returns the number of printed chars in a value of type `cint`. You 
suggest discarding the value and this works fine. But when I'm trying to 
actually capture the return value and print it, an error occurs: 
    
    
    proc printf(format: cstring): cint {.importc, varargs.}
    
    let n = printf("My name is %s and I am %d years old!\n", "Ben", 30)
    
    echo n
    
    
    
    Error: execution of an external compiler program 'clang -c  -w  
-I/Users/moigagoo/nim/lib -o /Users/moigagoo/Projects/nimcache/cffi.o 
/Users/moigagoo/Projects/nimcache/cffi.c' failed with exit code: 256
    
    /Users/moigagoo/Projects/nimcache/cffi.c:21:16: error: conflicting types 
for 'printf'
    N_NIMCALL(int, printf)(NCSTRING format0, ...);
                   ^
    /usr/include/stdio.h:259:6: note: previous declaration is here
    int      printf(const char * __restrict, ...) __printflike(1, 2);
             ^
    1 error generated.
    

If `cint` is `int32`, why does this happen?

The weird part is that the same error occurs even if I create a completely 
separate `cint` variable: 
    
    
    proc printf(format: cstring): cint {.importc, varargs.}
    
    discard printf("My name is %s and I am %d years old!\n", "Ben", 30)
    
    let n:cint = 123
    echo n
    

  1. Usage of `{.importc.}` for JavaScript interfacing is confusing. Why are we 
using the C pragma for JS? Why should every field be explicitly imported?
  2. Even more confusing is the usage of `{.importcpp.}`: “It instructs the 
compiler to generate JavaScript code which calls the specified procedure as if 
it was a member of the first argument’s object.” There's no obvious logic in 
using C++ pragma in JS interfacing, let alone using it to achieve this effect.


Reply via email to