# Splicing a full type name into expression

**URL:** <https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098>\
**Category:** New to Julia\
**Created:** [November 16, 2017, 10:59am UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098 "2017-11-16T10:59:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 16, 2017, 10:59am UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/1 "2017-11-16T10:59:49Z")

</div>

I have the following type inside a module:

```julia
module M struct T end end

```

Then the following works as expected, yielding an instance of M.T:

```julia
eval(:(M.T()))

```

Using the type indirectly also works, yielding again an instance of M.T:

```julia
stype = M.T
eval(:($stype()))

```

However, dumping the expression for indirect approach:

```julia
dump(:($stype()))

Expr
  head: Symbol call
  args: Array{Any}((1,))
    1: M.T <: Any
  typ: Any

```

it becomes obvious that it incorporated the entire type into expression tree. Comparing to the direct way:

```julia
dump(:(M.T()))

Expr
  head: Symbol call
  args: Array{Any}((1,))
    1: Expr
      head: Symbol .
      args: Array{Any}((2,))
        1: Symbol M
        2: QuoteNode
          value: Symbol T
      typ: Any
  typ: Any

```

And here we got a well-formed expression tree.  
Question: how to achieve the same effect (a well-formed expression tree) using the indirect approach, through the `stype` variable?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 16, 2017, 1:53pm UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/2 "2017-11-16T13:53:46Z")

</div>

> [@wrgr](#):
>
> a well-formed expression tree

There’s nothing not well formed about it.

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 16, 2017, 5:52pm UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/3 "2017-11-16T17:52:33Z")

</div>

But there is no easy way to achieve the exact correspondence between the trees? For my purposes the behavior of the `:(M.T())` is more desirable than that of `:($stype())`, but I need to use the dynamically supplied `stype::DataType` variable.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 16, 2017, 10:55pm UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/4 "2017-11-16T22:55:16Z")

</div>

It’s unclear what do you want with `:(M.T())` since it’s not really well defined. Or there’s basically no robust way to construct a expression with only symbols that can reliably refer to the right object in all evaluation context.

Another way to do it reliably would be `:($M.T())`.

If you want an expression that has only symbols you can have a look at [https://github.com/JuliaLang/julia/blob/81e245c850b18d871dbbfc6eebf21d129030ae53/test/testenv.jl#L31](https://github.com/JuliaLang/julia/blob/81e245c850b18d871dbbfc6eebf21d129030ae53/test/testenv.jl#L31). That code assume a particular evaluation context that’s useful for test (or mainly for printing purpose).

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 17, 2017, 7:26am UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/5 "2017-11-17T07:26:49Z")

</div>

Thank you for the answer, I don’t want anything with :(M.T()), I do want to (and need to) use :($stype()). The code you pointed me at only shows how to get the name of the current module. Is there any function which allows to get the module in which a type is defined? In this case I can construct Symbol-only expression manually, this seems to be the only way.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 17, 2017, 8:45am UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/6 "2017-11-17T08:45:08Z")

</div>

> [@wrgr](#):
>
> Is there any function which allows to get the module in which a type is defined?

```julia
julia> @which DateTime                                                                                                                                                   
Base.Dates                                                                                                                                                               

```

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 17, 2017, 8:58am UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/7 "2017-11-17T08:58:53Z")

</div>

Thank you for the answer, @mauro3, but this doesn’t work really:

```julia
stype = M.T
@which stype
# output:
Main

```

So it returns a module in which module `M` is defined, in this case `Main`. I’m asking about module in which the type `T` is defined.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 17, 2017, 11:07am UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/8 "2017-11-17T11:07:47Z")

</div>

Yep, you’re right, it gives the module in which the binding is defined. This works `stype.name.module`.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 17, 2017, 11:47am UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/9 "2017-11-17T11:47:56Z")

</div>

> [@wrgr](#):
>
> it becomes obvious that it incorporated the entire type into expression tree.

There’s nothing wrong with inserting literal objects into expressions, like `:($stype)` or `:($M.stype)`. I do it all the time. Why do you want to avoid it? If you construct `:(ModuleName.type)`, you’ll have problems in places where `ModuleName` is not imported, or when it’s shadowed by a local variable.

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 17, 2017, 12:44pm UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/10 "2017-11-17T12:44:06Z")

</div>

@cstjean I was surprised actually that it works at all, and thought that this is not a correct way of doing this.  
So what happens to this literal object when

- expression is evaluated? Literal objects are just inserted in right places, while others constructed from symbolic representations? In this case the expression acts as a closure, allowing to insert objects from elsewhere?
- expression is converted to a string (if you want some generated code to stay as a physical file)?

---

<div class="post-metadata">

**Author:** ![wrgr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wrgr/32/2796_2.png) [@wrgr](https://discourse.julialang.org/u/wrgr)\
**Post date:** [November 17, 2017, 12:51pm UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/11 "2017-11-17T12:51:44Z")

</div>

@mauro3 thanks for the tip, this indeed will allow to express a DataType in symbolic form. Btw, how to find out things like this (that DataType has a field `name`)? Documentation explorer yielded nothing about DataType and its internals, `fieldnames()` returns fields of the DataType itself.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 17, 2017, 12:54pm UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/12 "2017-11-17T12:54:07Z")

</div>

> [@wrgr](#):
>
> expression is evaluated? Literal objects are just inserted in right places, while others constructed from symbolic representations? In this case the expression acts as a closure, allowing to insert objects from elsewhere?

That works exactly as you would expect. In fact, it simplifies the compiler’s job, since it knows exactly what object is there, instead of having to look it up. It’s just a pointer to an object - there’s no memory issue.

> [@](#):
>
> expression is converted to a string (if you want some generated code to stay as a physical file)?

If you just want to print it, it kinda works, but obviously if you want to `eval`/`parse` that code, you’re likely to have problems with some objects (types are probably fine?). You could also try JLD.jl/JLD2.jl instead of printing it.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 17, 2017, 1:05pm UTC](https://discourse.julialang.org/t/splicing-a-full-type-name-into-expression/7098/13 "2017-11-17T13:05:45Z")

</div>

`fieldnames(DataType)` works. Also `dump` is of use. But often hitting Tab works. This is what I did:

```julia
julia> Int. # Tab Tab                                                                                                                                                            
abstract hasfreetypevars isleaftype llvm::DIType mutable ninitialized size types                                       
depth instance layout llvm::StructType name parameters super uid                                         
julia> Int.name. # Tab Tab                                                                                                                                                   
cache hash linearcache module mt name names wrapper                                                                        
julia> Int.name.module                                                                                                                                                   
Core                                                                                                                                                                     

```

Aside, note that these internals may change without deprecation from one version to the next.
