# How to build a string for icxx?

**URL:** <https://discourse.julialang.org/t/how-to-build-a-string-for-icxx/7354>\
**Category:** New to Julia\
**Tags:** cxx\
**Created:** [November 28, 2017, 11:07am UTC](https://discourse.julialang.org/t/how-to-build-a-string-for-icxx/7354 "2017-11-28T11:07:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [November 28, 2017, 11:07am UTC](https://discourse.julialang.org/t/how-to-build-a-string-for-icxx/7354/1 "2017-11-28T11:07:01Z")

</div>

Hi - I’m using the Cxx.jl package to get at data using a C++ library. I have a line that looks like this…

```julia
tracks = icxx"return $(he.galleryEvent).getValidHandle<gm2truth::TrackingActionArtRecordCollection>($(trackingActionSpect.artInputTag));"

```

This all works great, including the interpolation. The problem is that the type in the template can change and I want that to be set by a string. That is I want something like…

```julia
s = "gm2truth::SomeOtherType" # julia string
otherThings = icxx"return $(he.galleryEvent).getValidHandle<$s>($(trackingActionSpect.artInputTag));"

```

Of course this doesn’t work because `$s` isn’t a C++ type. It’s a string. But I want it to be the type that the string says.

Is there a way to do this? Thanks!!! – Adam

---

<div class="post-metadata">

**Author:** ![cdsousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdsousa/32/215553_2.png) [@cdsousa](https://discourse.julialang.org/u/cdsousa)\
**Post date:** [November 28, 2017, 11:40am UTC](https://discourse.julialang.org/t/how-to-build-a-string-for-icxx/7354/2 "2017-11-28T11:40:21Z")

</div>

Ok, this is a tough one.

Try something like:

```julia
s = "gm2truth::SomeOtherType" # julia string
code = "return \$(he.galleryEvent).getValidHandle<$s>(\$(trackingActionSpect.artInputTag));"
otherThings = @eval @icxx_str $code

```

Notice, in `code` construction, the **`\$`** before C++ objects and **`$`** before pure code string.

---

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [November 28, 2017, 7:55pm UTC](https://discourse.julialang.org/t/how-to-build-a-string-for-icxx/7354/3 "2017-11-28T19:55:04Z")

</div>

Hi - Thanks very much for the good suggestion. Note that `@icxx_std` should be `@icxx_str`. This works in the global REPL but not in a function. …

```julia
function getRecord(he::HeistEvent, rs::HeistRecordSpec)

   code = "return \$(he.galleryEvent).getValidHandle<$(rs.productName)>(\$(rs.artInputTag));"
   println(code)
   return @eval @icxx_str $code
end

```

Then in the REPL.

```julia
...
tracks = getRecord(hev, tas)

# The function prints out the code string -- looks good to me
return $(he.galleryEvent).getValidHandle<gm2truth::TrackingActionArtRecordCollection>($(rs.artInputTag));

# But then...
ERROR: LoadError: UndefVarError: he not defined
Stacktrace:
 [1] getRecord(::Heist.HeistEvent, ::Heist.HeistRecordSpec) at /Users/lyon/Development/julia/julia-centos6-prod8/mypkgs/Heist.jl/src/Heist.jl:111
 [2] include_from_node1(::String) at ./loading.jl:576
 [3] include(::String) at ./sysimg.jl:14
 [4] process_options(::Base.JLOptions) at ./client.jl:305
 [5] _start() at ./client.jl:371
while loading /vagrant/heistTest.jl, in expression starting on line 20

```

It doesn’t seem to understand the function argument.

Indeed this seems to be a tough one!

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [November 28, 2017, 8:20pm UTC](https://discourse.julialang.org/t/how-to-build-a-string-for-icxx/7354/4 "2017-11-28T20:20:50Z")

</div>

The text itself needs to be fixed at macro expansion time, though you can use `@eval` to build the text up dynamically. That said, the best solution to your problem is probably to use the julia-side representation of the C++ type:

```julia
s = cxxt"gm2truth::SomeOtherType"
tracks = icxx"return $(he.galleryEvent).getValidHandle<$s>($(trackingActionSpect.artInputTag));"

```

which should work well, because the text is fixed at macro expansion time (though the `s` is allowed to vary at runtime).

---

<div class="post-metadata">

**Author:** ![lyonsquark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyonsquark/32/4483_2.png) [@lyonsquark](https://discourse.julialang.org/u/lyonsquark)\
**Post date:** [November 29, 2017, 3:01pm UTC](https://discourse.julialang.org/t/how-to-build-a-string-for-icxx/7354/5 "2017-11-29T15:01:53Z")

</div>

Thanks!! That’s what I needed to know. Here’s what I ended up with…

```julia
function getRecord(he::HeistEvent, rs::HeistRecordSpec)
   s = @eval @cxxt_str $(rs.productName)
   return icxx"return $(he.galleryEvent).getValidHandle<$s>($(rs.artInputTag));"
end

```

@Keno & @cdsousa - I appreciate your help – Adam
