Thanks, LeuGim. **`When`** does work. However 
[https://nim-lang.org/docs/manual.html#statements-and-expressions-when-statement](https://nim-lang.org/docs/manual.html#statements-and-expressions-when-statement)
 still be Greek for me.

* * *

more question I, a lazy none programmer practitioner, met: How to make an 
auto-converted type? Sorry, I don't know its exact name. I will explain it as 
folows:

for example, in the original functions in DLL, Widecstring type arg is need. So 
the actual code should be
    
    
    proc original_Clang_FunctionA(value: Widecstring) {.cdecl,
        importc: "functon1", dynlib: xldll}.
    
    ...
    proc original_Clang_FunctionZ(value: Widecstring) {.cdecl,
        importc: "functon26", dynlib: xldll}.
    
    
    var value1 = newWideCString(convert("你好", "utf8", "gb2312"))
    original_Clang_FunctionA(value1)
    

or we can make a function 
    
    
    proc s(val: string|Widecstring): Widecstring =
        when not type(val) is Widecstring:
            return newWideCString(convert(val, "utf8", "gb2312"))
        else:
            return val
    

then user writes 
    
    
    original_Clang_FunctionA("你好".s)
    ...
    
    original_Clang_FunctionZ("世界".s)
    ...
    
    

both are tedious for user.

So We can wrap the DLL function like this:
    
    
    proc original_Clang_FunctionA(value: Widecstring) {.cdecl,
        importc: "functon26", dynlib: xldll}.
    proc FunctionA(value: string|Widecstring) =
        var value1 = value
        when not type(value1) is Widecstring:
            value1 = newWideCString(convert(value1, "utf8", "gb2312"))
        original_Clang_FunctionA(value1)
    
    

then the user only need to write in a clean way: 
    
    
    FunctionA("你好")
    

However the problem is that we have to supply every wrapped functions for so 
many functions, which is a repetitive work for library supplier.

So if there is something like this
    
    
    type
        someString from object(string|Widecstring) =
            when not type(someString) is Widecstring:
                someString.value = newWideCString(convert(value1, "utf8", 
"gb2312"))
                someString.type = Widecstring
    
    proc FunctionA(value: someString) {.cdecl,
        importc: "functon1", dynlib: xldll}.
    
    ...
    proc FunctionZ(value: someString) {.cdecl,
        importc: "functon26", dynlib: xldll}.
    

and the user only writes like before 
    
    
    FunctionA("你好")
    ...
    FunctionA("世界")
    
    

is this possible? Thanks 

Reply via email to