Crystal::Macros::FunDef - Crystal 1.20.2

# class Crystal::Macros::FunDef

- Crystal::Macros::FunDef
- Crystal::Macros::ASTNode

##
Overview

A function declaration inside a lib, or a top-level C function definition.

Every function `node` is equivalent to:

    fun {{ node.name }} {% if real_name = node.real_name %}= {{ real_name }}{% end %}(
      {% for arg in node.args %} {{ arg }}, {% end %}
      {% if node.variadic? %} ... {% end %}
    ) {% if return_type = node.return_type %}: {{ return_type }}{% end %}
    {% if node.has_body? %}
      {{ body }}
    end
    {% end %}

##
Defined in:

compiler/crystal/macros.cr

##
Instance Method Summary

- #args : ArrayLiteral(Arg)

  Returns the parameters of the function.

- #body : ASTNode | Nop

  Returns the body of the function, if any.

- #has_body? : BoolLiteral

  Returns whether this function has a body.

- #name : MacroId

  Returns the name of the function in Crystal.

- #real_name : StringLiteral | Nop

  Returns the real C name of the function, if any.

- #return_type : ASTNode | Nop

  Returns the return type of the function, if specified.

- #variadic? : BoolLiteral

  Returns whether the function is variadic.

### Instance methods inherited from class `Crystal::Macros::ASTNode`

!=(other : ASTNode) : BoolLiteral !=, ==(other : ASTNode) : BoolLiteral ==, class_name : StringLiteral class_name, column_number : StringLiteral | NilLiteral column_number, doc : StringLiteral doc, doc_comment : MacroId doc_comment, end_column_number : StringLiteral | NilLiteral end_column_number, end_line_number : StringLiteral | NilLiteral end_line_number, filename : StringLiteral | NilLiteral filename, id : MacroId id, is_a?(type : TypeNode) : BoolLiteral is_a?, line_number : StringLiteral | NilLiteral line_number, nil? : BoolLiteral nil?, raise(message) : NoReturn raise, stringify : StringLiteral stringify, symbolize : SymbolLiteral symbolize, warning(message : StringLiteral) : NilLiteral warning

##
Instance Method Detail

def args : ArrayLiteral(Arg) #

Returns the parameters of the function.

This does not include the variadic parameter.

def body : ASTNode | Nop #

Returns the body of the function, if any.

Both top-level funs and lib funs may return a `Nop`. Instead, `#has_body?` can be used to distinguish between the two.

    macro body_class(x)
      {{ (x.is_a?(LibDef) ? x.body : x).body.class_name }}
    end

    body_class(lib MyLib
      fun foo
    end) # => "Nop"

    body_class(fun foo
    end) # => "Nop"

def has_body? : BoolLiteral #

Returns whether this function has a body.

Top-level funs have a body, whereas lib funs do not.

    macro has_body(x)
      {{ (x.is_a?(LibDef) ? x.body : x).has_body? }}
    end

    has_body(lib MyLib
      fun foo
    end) # => false

    has_body(fun foo
    end) # => true

def name : MacroId #

Returns the name of the function in Crystal.

def real_name : StringLiteral | Nop #

Returns the real C name of the function, if any.

def return_type : ASTNode | Nop #

Returns the return type of the function, if specified.

def variadic? : BoolLiteral #

Returns whether the function is variadic.