# Doing crazy stuff with generics

**URL:** <https://discourse.julialang.org/t/doing-crazy-stuff-with-generics/78225>\
**Category:** General Usage\
**Created:** [March 21, 2022, 4:29pm UTC](https://discourse.julialang.org/t/doing-crazy-stuff-with-generics/78225 "2022-03-21T16:29:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [March 21, 2022, 4:29pm UTC](https://discourse.julialang.org/t/doing-crazy-stuff-with-generics/78225/1 "2022-03-21T16:29:11Z")

</div>

Hi all 🙂

Why can’t I do the following:

```julia
julia> function crazy(T)
           if T == Int
               String
           elseif T == String
               Int
           else
               error("use Int or String")
           end
       end
crazy (generic function with 1 method)

julia> struct B{T <: Union{Int, String}}
           a::crazy(T)
       end
ERROR: use Int or String
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] crazy(T::TypeVar)
   @ Main .\REPL[3]:7
 [3] top-level scope
   @ REPL[4]:1

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 21, 2022, 4:32pm UTC](https://discourse.julialang.org/t/doing-crazy-stuff-with-generics/78225/2 "2022-03-21T16:32:03Z")

</div>

> [@Impressium](#):
>
> `a::crazy(T)`

you can’t call function here. more broadly, Julia doesn’t have dependent types, you can’t compute the type of a field based on type parameters

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 21, 2022, 4:32pm UTC](https://discourse.julialang.org/t/doing-crazy-stuff-with-generics/78225/3 "2022-03-21T16:32:46Z")

</div>

You can sort of do this with [GitHub - vtjnash/ComputedFieldTypes.jl: Build types in Julia where some fields have computed types](https://github.com/vtjnash/ComputedFieldTypes.jl) but it would be great to have this work in base Julia too.
