Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Sourcery template to generate a default initializer in your `struct`s (working in version 0.8.0)
{% for type in types.structs %}
{% if type|annotated:"AutoInit" %}
{% set spacing %}{% if type.parentName %} {% endif %}{% endset %}
{% map type.storedVariables into parameters using var %}{{ var.name }}: {{ var.typeName }}{% endmap %}
// sourcery:inline:auto:{{ type.name }}.AutoInit
{{spacing}} {{ type.accessLevel }} init({{ parameters|join:", " }}) { // swiftlint:disable:this line_length
{{spacing}} {% for variable in type.storedVariables %}
{{spacing}} self.{{ variable.name }} = {{ variable.name }}
{{spacing}} {% endfor %}
{{spacing}} }
// sourcery:end
{% endif %}
{% endfor %}
@txuslee

This comment has been minimized.

Copy link

@txuslee txuslee commented Dec 14, 2017

Since last Sourcery update (0.10), the stencil generates an invalid initializer. Something may have changed in 'join' filter because now it doesn't append a comma by default. You must force the comma with an argument: {{ parameters|join:", " }}

And thanks for this great template, it's been very useful!

@ferologics

This comment has been minimized.

Copy link

@ferologics ferologics commented Dec 14, 2020

Thank you so much @txuslee for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment