Hi,
I use RefValue{Any}
to protect a scalar (immutable type) inside the IdDict
, according to Embedding Julia · The Julia Language. But I don’t know how to use the RefValue{Any}
variable ('b" in the following example) in C (access its value , add something to its value, etc.). Below is a simple example:
#include <stdlib.h>
#include <julia.h>
int main(void) {
jl_init();
// gc protect
jl_value_t * refs = jl_eval_string("refs = IdDict()");
jl_function_t * setindex = jl_get_function(jl_base_module, "setindex!");
jl_datatype_t* ref_type = (jl_datatype_t*)jl_eval_string("Base.RefValue{Any}");
jl_value_t * a = jl_eval_string("[1.0,2.0,3.0,4.0]");
jl_call3(setindex, refs, a, a);
jl_value_t * b = 0;
jl_value_t * ref_b = 0;
{
b = jl_box_float64(0.0001);
JL_GC_PUSH1(&b);
ref_b = jl_new_struct(ref_type, b);
JL_GC_POP();
jl_call3(setindex, refs, ref_b, ref_b);
}
jl_function_t * print = jl_get_function(jl_base_module, "show");
jl_function_t * vcat = jl_get_function(jl_base_module, "vcat");
printf("print a:\n");
jl_call1(print, (jl_value_t *)a);
printf("\n");
printf("print ref_b:\n");
jl_call1(print, ref_b);
printf("\n");
jl_value_t * c = jl_call2(vcat, (jl_value_t *)a, (jl_value_t *)ref_b);
jl_call3(setindex, refs, c, c);
printf("print vcat(a, ref_b):\n");
jl_call1(print, c);
printf("\n");
jl_atexit_hook(0);
return 0;
}
And the output is:
print a:
[1.0, 2.0, 3.0, 4.0]
print ref_b:
Base.RefValue{Any}(0.0001)
print vcat(a, ref_b):
Any[1.0, 2.0, 3.0, 4.0, Base.RefValue{Any}(0.0001)]
Basically, I want to protect immutable variable b
. So I wrap it in RefValue{Any}
as ref_b
, and then push it to refs
, which is an IdDict
. But when I want to call vcat
to concatenate a
and ref_b
, ref_b
is Base.RefValue{Any}(0.0001)
instead of 0.0001, I have googled extensivly but found nothing. How can I get the value of b (=0.0001) instead of Base.RefValue{Any}(0.0001)
through ref_b
?
Thanks!