Search code examples
red-langred-system

How do you use #call directive for Red function with string datatypes as parameters?


Given a script, such as below, which does not compile yet, how can I use #call to use my Red function from within Red/System?

Red []

pff: function [a [string!] ][print a]  

#system [
    #call [pff "hello"]    
]

There is a type mismatch. What do you need to do to convert the string to the proper Red/System datatype?


Solution

  • To expand on Peter's correct answer, in this particular case, you need to internalize the c-string as a red-string!, using string/load, like this:

    Red []
    
    pff: function [a [string!] ][print a]  
    
    #system [
        s: "hello"
        hello: string/load s 1 + length? s UTF-8
        #call [pff hello]
    ]
    

    Notes:

    • Red/System's strings are ASCII only for now, so you need to specify UTF-8 as the source encoding format.

    • You need to pass the size of the c-string accounting for the terminal NUL (hence the + 1).

    The internal API is not documented as it is not finalized. Feel free to ask here or on our other communication channels if you need any info about it.