# DataType to String

**URL:** <https://discourse.julialang.org/t/datatype-to-string/82405>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 8, 2022, 6:04am UTC](https://discourse.julialang.org/t/datatype-to-string/82405 "2022-06-08T06:04:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chiion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiion/32/37002_2.png) [@chiion](https://discourse.julialang.org/u/chiion)\
**Post date:** [June 8, 2022, 6:04am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/1 "2022-06-08T06:04:22Z")

</div>

Hi All,  
I’m writing a module with a function that converts an internal DataType into a string.  
However, without importing/exporting the DataType, the module name is added to the string.

Example:

```julia
module A
    struct T1 end
    function to_string()
        s = T1()
        return string(typeof(s))
    end
end

julia> A.to_string()
"Main.A.T1"

julia> import .A.T1; A.to_string()
"T1"

```

In this example, is there a way for `to_string()` to return `"T1"` without importing or exporting `T1` from module `A`?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [June 8, 2022, 8:40am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/2 "2022-06-08T08:40:19Z")

</div>

```julia
julia> module Foo
       struct Bar end
       end
Main.Foo

julia> typeof(Foo.Bar())
Main.Foo.Bar

julia> string(nameof(typeof(Foo.Bar())))
"Bar"

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 8, 2022, 8:44am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/3 "2022-06-08T08:44:18Z")

</div>

> [@pfitzseb](#):
>
> `string(nameof(typeof(Foo.Bar())))`

```julia
julia> string(nameof(Foo.Bar))
"Bar"

```

Is there any reason to construct an object, just to retrieve the name of its type, when one is explicitly using the type anyway?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [June 8, 2022, 9:04am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/4 "2022-06-08T09:04:56Z")

</div>

No. Depends on whether your function is supposed to operate on instances or types and OP’s `to_string` kinda sorta does the former…

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 8, 2022, 9:07am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/5 "2022-06-08T09:07:12Z")

</div>

Not sure. The code directly references `T1`, and it seems an unnecessary detour to put `s = T1(); typeof(s)`, just to get back to `T1`. I guess @chiion needs to clarify.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [June 8, 2022, 9:18am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/6 "2022-06-08T09:18:40Z")

</div>

Eh, feels like you’re overthinking this. @chiion will be able to figure out which one’s more appropriate for their use case.

---

<div class="post-metadata">

**Author:** ![chiion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiion/32/37002_2.png) [@chiion](https://discourse.julialang.org/u/chiion)\
**Post date:** [June 8, 2022, 9:24am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/7 "2022-06-08T09:24:40Z")

</div>

Thanks, that’s exactly what I need.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 8, 2022, 9:38am UTC](https://discourse.julialang.org/t/datatype-to-string/82405/8 "2022-06-08T09:38:46Z")

</div>

> [@pfitzseb](#):
>
> Eh, feels like you’re overthinking this.

I dunno. I have seen a _lot_ of weird detours in code, so I like to point it out. Oh well.
