# Generate function code

**URL:** https://discourse.julialang.org/t/generate-function-code/67385
**Category:** General Usage
**Tags:** metaprogramming
**Created:** [August 30, 2021, 11:40pm UTC](https://discourse.julialang.org/t/generate-function-code/67385 "2021-08-30T23:40:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [August 30, 2021, 11:40pm UTC](https://discourse.julialang.org/t/generate-function-code/67385/1 "2021-08-30T23:40:20Z")

</div>

Hellow!

I have

```julia
type_list = [
    # type code
    #
    Int64 0x11;
    Int32 0xa2;
    Int16 0x33;
    Int8 0x24;

    #
    UInt64 0x15;
    UInt32 0xb6;
    UInt16 0x7a;
    UInt8 0x8f;

    #
    Float64 0x92;
    Float32 0x0a;
    Float16 0x0b;
]

```

I need:

```julia
function get_type_by_code(code::UInt8)
    code == 0x11 && return Int64
    code == 0xa2 && return Int32
    code == 0x33 && return Int16
    code == 0x24 && return Int8
    code == 0x15 && return UInt64
    code == 0xb6 && return UInt32
    code == 0x7a && return UInt16
    code == 0x8f && return UInt8
    code == 0x92 && return Float64
    code == 0x0a && return Float32
    code == 0x0b && return Float16
end

```

I do:

```julia
code_string = "function get_type_by_code(code::UInt8)\n"
for i = 1 : length(type_list[:,1])
    code_string *= " code == $(type_list[i,2]) && return $(type_list[i,1])\n"
end
code_string *= "end"
Base.include_string(Main, code_string)

```

how to make it better?

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [August 31, 2021, 1:38am UTC](https://discourse.julialang.org/t/generate-function-code/67385/2 "2021-08-31T01:38:15Z")

</div>

Firstly, you should use a Dict rather than a Matrix. Once you do, is this good enough?

```julia
get_type_by_code(code) = type_list[code]

```

---

<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: [August 31, 2021, 1:41am UTC](https://discourse.julialang.org/t/generate-function-code/67385/3 "2021-08-31T01:41:00Z")

</div>

```julia
const type_list = Dict(
     0x11 => Int64,
     0xa2 => Int32,
     0x33 => Int16,
     0x24 => Int8,

     0x15 => UInt64,
     0xb6 => UInt32,
     0x7a => UInt16,
     0x8f => UInt8,

     0x92 => Float64,
     0x0a => Float32,
     0x0b => Float16,
)

```

and then just `type_list[0x11]` to get `Int64`

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [August 31, 2021, 8:16am UTC](https://discourse.julialang.org/t/generate-function-code/67385/4 "2021-08-31T08:16:25Z")

</div>

No, i need exactly this function by example:

```julia
function get_type_by_code(code::UInt8)
    code == 0x11 && return Int64
    code == 0xa2 && return Int32
    code == 0x33 && return Int16
    code == 0x24 && return Int8
    code == 0x15 && return UInt64
    code == 0xb6 && return UInt32
    code == 0x7a && return UInt16
    code == 0x8f && return UInt8
    code == 0x92 && return Float64
    code == 0x0a && return Float32
    code == 0x0b && return Float16
end

```

```julia
const type_list2 = Dict(
     0x11 => Int64,
     0xa2 => Int32,
     0x33 => Int16,
     0x24 => Int8,

     0x15 => UInt64,
     0xb6 => UInt32,
     0x7a => UInt16,
     0x8f => UInt8,

     0x92 => Float64,
     0x0a => Float32,
     0x0b => Float16,
)

using BenchmarkTools
@btime type_list2[0x92] # 1.900 ns (0 allocations: 0 bytes)
@btime get_type_by_code(0x92) # 0.001 ns (0 allocations: 0 bytes)

```

this is done for convenience and speed, the function is not one,  
I edit only main list, then code generation

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [August 31, 2021, 8:18am UTC](https://discourse.julialang.org/t/generate-function-code/67385/5 "2021-08-31T08:18:21Z")

</div>

Я делаю:

```julia
code_string = "function get_type_by_code(code::UInt8)\n"
for i = 1 : length(type_list[:,1])
    code_string *= " code == $(type_list[i,2]) && return $(type_list[i,1])\n"
end
code_string *= "end"
Base.include_string(Main, code_string)

```

как сделать лучше?

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [August 31, 2021, 9:55pm UTC](https://discourse.julialang.org/t/generate-function-code/67385/6 "2021-08-31T21:55:55Z")

</div>

> [@andrey2185](#):
>
> ```julia
> @btime type_list2[0x92] # 1.900 ns (0 allocations: 0 bytes)
> @btime get_type_by_code(0x92) # 0.001 ns (0 allocations: 0 bytes)
> 
> ```

There’s no such thing as pico-second function evaluations (this is a common confusion, and the result of constant propagation in the compiler). Using the `Ref` trick from the `BenchmarkTools` docs, and placing the Dict into a function for similar effect, we see they’re much more similar than that:

```julia
julia> get_type_by_code_dict(x) = type_list2[x];

julia> @btime get_type_by_code_dict($(Ref(0x92))[])
  6.828 ns (0 allocations: 0 bytes)
Float64

julia> @btime get_type_by_code($(Ref(0x92))[])
  3.266 ns (0 allocations: 0 bytes)
Float64

```

You can use a Base.ImmutableDict to regain that speed in this case:

```julia
julia> const type_list_imm = Base.ImmutableDict(
            0x11 => Int64,
            0xa2 => Int32,
            0x33 => Int16,
            0x24 => Int8,

            0x15 => UInt64,
            0xb6 => UInt32,
            0x7a => UInt16,
            0x8f => UInt8,

            0x92 => Float64,
            0x0a => Float32,
            0x0b => Float16,
       );

julia> get_type_by_code_imdict(x) = type_list_imm[x];

julia> @btime get_type_by_code_imdict($(Ref(0x92))[])
  3.292 ns (0 allocations: 0 bytes)
Float64

julia> @btime get_type_by_code($(Ref(0x92))[])
  3.268 ns (0 allocations: 0 bytes)
Float64

```

That said, if you really want to create such a function from your `Dict`, you should not use strings but rather `Expr`s (expressions). The Metaprogramming section of the julia manual is a great place to start learning how to do this.

---

<div class="post-metadata">

### Author: ![serhii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/serhii/32/13719_2.png) [@serhii](https://discourse.julialang.org/u/serhii)
#### Post date: [January 27, 2022, 9:31pm UTC](https://discourse.julialang.org/t/generate-function-code/67385/7 "2022-01-27T21:31:34Z")

</div>

How about value [types](https://docs.julialang.org/en/v1/manual/types/#%22Value-types%22)? One essentially can do pattern matching with them just by using multiple dispatch feature.
