What does that mean _ = """ is a comment or what?

I want to know the purpose of this

 _ = """
 
"""

A sample code like this in a module:

_ = """
schema "my_schema" do
 field :name, :string
 timestamps()
end
"""

It assigns the Multiline string to the unnamed variable and forgets it’s afterwards.

This has no effect for immediate values, though functions on the RHS will be called, all sideffects included.

Also in this case the string will be compiled into the modules byte code.

I’d not consider this a comment.

If it’s meant to be a replacement for Multiline comments, then the author of that code should probably rethink their position.

7 Likes

This code will run?

Which code?

The thing you posted is valid code, it will assign a string literal to the unnamed variable.

Of course it will not run the code that is inside the string, as it will also not run that code when assigning it to a named variable, it’s just a string…

BUT the string itself will be in the compiled module.

1 Like

Nice, Thank’s for reply @NobbZ!

1 Like

It will only assign a string to a variable and forget it immediately after.

As @NobbZ said, if there was a function call after the = then yes, it would have been executed (and the return value of the function call will be discarded immediately after).

1 Like

This is sometimes helpful in generated code to avoid “unused variable” warnings.

1 Like

But you can use the generated: true option in macros.

True true, but there’s a lot of code that can be generated without requiring macros. I would say over 90% of the code I generate in the ex_cldr family - and there are hundreds if not thousands of generated function heads - doesn’t come from macros.

2 Likes