# Automatically generating boilerplate code

**URL:** <https://discourse.julialang.org/t/automatically-generating-boilerplate-code/23180>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [April 16, 2019, 2:18am UTC](https://discourse.julialang.org/t/automatically-generating-boilerplate-code/23180 "2019-04-16T02:18:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Post date:** [April 16, 2019, 2:18am UTC](https://discourse.julialang.org/t/automatically-generating-boilerplate-code/23180/1 "2019-04-16T02:18:32Z")

</div>

I have the following repetitive code structure.

```julia
mutable struct SC011
    state::SCBit
    info::String
    nin::Int
    nout::Int
    SC011() = new(false, "(1-λ₁)/(1-λ₁λ₂)", 2, 1)
end

Base.broadcastable(g::SC011) = Ref(g)

function proc(g::SC011, b1::SCBit, b2::SCBit)
    if !(ismissing(b1) || ismissing(b2))
        g.state = !b1 | g.state & b2
        return g.state
    end
    handle_missing_two_inputs("SC011", b1, b2)
end

```

… plus a few other methods defined for `SC011`.

I have dozens of these to implement, where the only change from one type to the next is the type name `SC011`, the string `"(1-λ₁)/(1-λ₁λ₂)"`, and the expression ` !b1 | g.state & b2`.

It would obviously be great to automate all this. I was hoping `@generated` would be the key, but it seems macros can’t be used to define a new type. Maybe I just don’t know what I’m doing. I remember seeing a blog post where someone automated a lot of boilerplate code using reasonably simple metaprogramming, but I can’t find it now and I’m not sure if it’s relevant here.

Is automating this possible, and if so can anyone provide a pointer to a similar example please? Many thanks.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 16, 2019, 2:26am UTC](https://discourse.julialang.org/t/automatically-generating-boilerplate-code/23180/2 "2019-04-16T02:26:43Z")

</div>

You can loop over `(name,string,expr)` and use `@eval` to generate the code

```nohighlight
for (name,string,expr)∈((:SC011,"(1-λ₁)/(1-λ₁λ₂)",:(!b1 | g.state & b2)),)
	@eval begin
		mutable struct $name
			state::SCBit
			info::String
			nin::Int
			nout::Int
			$name() = new(false, $string, 2, 1)
		end

		Base.broadcastable(g::$name) = Ref(g)

		function proc(g::$name, b1::SCBit, b2::SCBit)
			if !(ismissing(b1) || ismissing(b2))
				g.state = $expr
				return g.state
			end
			handle_missing_two_inputs("$name", b1, b2)
		end
	end
end

```

---

<div class="post-metadata">

**Author:** ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Post date:** [April 16, 2019, 2:44am UTC](https://discourse.julialang.org/t/automatically-generating-boilerplate-code/23180/3 "2019-04-16T02:44:13Z")

</div>

Wow, the people on this site are amazing!
