class Crystal::Def

Overview

A method definition.

'def' [ receiver '.' ] name
  body
'end'

| 'def' [ receiver '.' ] name '(' [ arg [ ',' arg ]* ] ')' body 'end'

Included Modules

Direct Known Subclasses

Defined in:

compiler/crystal/codegen/ast.cr
compiler/crystal/macros/methods.cr
compiler/crystal/semantic/ast.cr
compiler/crystal/semantic/bindings.cr
compiler/crystal/semantic/default_arguments.cr
compiler/crystal/semantic/hooks.cr
compiler/crystal/semantic/new.cr
compiler/crystal/semantic/warnings.cr
compiler/crystal/syntax/ast.cr
compiler/crystal/tools/doc/to_json.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Crystal::HookExpansionsContainer

add_hook_expansion(node) add_hook_expansion, hook_expansions : Array(ASTNode) | Nil hook_expansions

Instance methods inherited from module Crystal::Annotatable

add_annotation(annotation_type : AnnotationType, value : Annotation) add_annotation, all_annotations : Array(Annotation) | Nil all_annotations, annotation(annotation_type : AnnotationType) : Annotation | Nil annotation, annotations(annotation_type : AnnotationType) : Array(Annotation) | Nil
annotations : Hash(AnnotationType, Array(Annotation)) | Nil
annotations
, annotations=(annotations : Hash(AnnotationType, Array(Annotation)) | Nil) annotations=

Instance methods inherited from class Crystal::ASTNode

accept(visitor) accept, accept_children(visitor) accept_children, add_observer(observer) add_observer, at(node : ASTNode)
at(location : Location | Nil)
at
, at_end(node : ASTNode)
at_end(end_location : Location | Nil)
at_end
, bind(from = nil, &) bind, bind_to(node : ASTNode) : Nil
bind_to(nodes : Indexable) : Nil
bind_to
, class_desc class_desc, class_name class_name, clone clone, dependencies : SmallNodeList dependencies, doc doc, doc=(doc) doc=, enclosing_call : Call | Nil enclosing_call, enclosing_call=(enclosing_call : Call | Nil) enclosing_call=, end_location : Location | Nil end_location, end_location=(end_location : Location | Nil) end_location=, false_literal? false_literal?, filtered_by(filter) filtered_by, find_owner_trace(program, owner) find_owner_trace, freeze_type freeze_type, inspect(io : IO) : Nil inspect, interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode) | Nil, block : Crystal::Block | Nil, interpreter : Crystal::MacroInterpreter, name_loc : Location | Nil) interpret, interpret_compare(other) interpret_compare, length : Int32 | Nil length, location : Location | Nil location, location=(location : Location | Nil) location=, macro_is_a?(macro_type : MacroType) : Bool macro_is_a?, map_type(type) map_type, name_location name_location, name_size name_size, no_returns? no_returns?, nop? nop?, notify_observers notify_observers, pretty_print(pp) pretty_print, propagate propagate, raise(message, inner = nil, exception_type = Crystal::TypeException) raise, raise_frozen_type(freeze_type, invalid_type, from) raise_frozen_type, remove_enclosing_call(enclosing_call) remove_enclosing_call, remove_observer(observer) remove_observer, restrict_type_to_freeze_type(freeze_type, type) restrict_type_to_freeze_type, restriction_of?(other : Underscore, owner, self_free_vars = nil, other_free_vars = nil)
restriction_of?(other : ASTNode, owner, self_free_vars = nil, other_free_vars = nil)
restriction_of?(other : Type, owner, self_free_vars = nil, other_free_vars = nil)
restriction_of?(other, owner, self_free_vars = nil, other_free_vars = nil)
restriction_of?
, set_enclosing_call(enclosing_call) set_enclosing_call, set_type(type : Type)
set_type(type : Nil)
set_type
, set_type_from(type, from) set_type_from, simple_literal? simple_literal?, single_expression single_expression, single_expression? single_expression?, stringify stringify, supports_autocast?(number_autocast : Bool = true) supports_autocast?, symbolize symbolize, to_macro_id to_macro_id, to_s(io : IO, macro_expansion_pragmas = nil, emit_doc = false, emit_location_pragmas : Bool = false) : Nil to_s, to_string(context) to_string, transform(transformer) transform, true_literal? true_literal?, truthy? truthy?, type
type(*, with_autocast = false)
type
, type=(type) type=, type? type?, type_from_dependencies : Type | Nil type_from_dependencies, unbind_from(nodes : Nil)
unbind_from(node : ASTNode)
unbind_from(nodes : Enumerable(ASTNode))
unbind_from
, update(from = nil) update, visibility visibility, visibility=(visibility : Visibility) visibility=, warning(message, inner = nil, exception_type = Crystal::TypeException) warning, wrong_number_of(elements, subject, given, expected)
wrong_number_of(elements, given, expected)
wrong_number_of
, wrong_number_of_arguments(subject, given, expected) wrong_number_of_arguments, wrong_number_of_message(elements, subject, given, expected)
wrong_number_of_message(elements, given, expected)
wrong_number_of_message

Class methods inherited from class Crystal::ASTNode

class_desc : String class_desc

Instance methods inherited from class Reference

==(other : self)
==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup, hash(hasher) hash, initialize initialize, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference) : Bool
same?(other : Nil)
same?
, to_s(io : IO) : Nil to_s

Constructor methods inherited from class Reference

new new, unsafe_construct(address : Pointer, *args, **opts) : self unsafe_construct

Class methods inherited from class Reference

pre_initialize(address : Pointer) pre_initialize

Instance methods inherited from class Object

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash
hash
, in?(collection : Object) : Bool
in?(*values : Object) : Bool
in?
, inspect(io : IO) : Nil
inspect : String
inspect
, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil!
not_nil!
, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String
to_json
, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil
to_pretty_json
, to_s(io : IO) : Nil
to_s : String
to_s
, to_yaml(io : IO) : Nil
to_yaml : String
to_yaml
, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

Class methods inherited from class Object

from_json(string_or_io : String | IO, root : String)
from_json(string_or_io : String | IO)
from_json
, from_yaml(string_or_io : String | IO) from_yaml

Macros inherited from class Object

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

Constructor Detail

def self.new(name : String, args : Array(Crystal::Arg) = [] of Arg, body = nil, receiver : Crystal::ASTNode | Nil = nil, block_arg : Nil | Crystal::Arg = nil, return_type : Crystal::ASTNode | Nil = nil, macro_def : Bool = false, block_arity : Int32 | Nil = nil, abstract __arg26 : Bool = false, splat_index : Int32 | Nil = nil, double_splat : Nil | Crystal::Arg = nil, free_vars : Nil | Array(String) = nil) #

[View source]

Class Method Detail

def self.argless_initialize(instance_type) #

[View source]
def self.argless_new(instance_type) #

[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def abi_info=(abi_info : Bool) #

[View source]
def abi_info? : Bool #

[View source]
def abstract=(abstract __arg0 : Bool) #

[View source]
def abstract? : Bool #

[View source]
def accept_children(visitor) #

[View source]
def add_special_var(name) #

[View source]
def always_inline=(always_inline : Bool) #

true if this def has the @[AlwaysInline] annotation


[View source]
def always_inline? : Bool #

true if this def has the @[AlwaysInline] annotation


[View source]
def args : Array(Arg) #

[View source]
def args=(args : Array(Arg)) #

[View source]
def assigns_special_var=(assigns_special_var : Bool) #

[View source]
def assigns_special_var? : Bool #

[View source]
def autogenerated? #

[View source]
def block_arg : Arg | Nil #

[View source]
def block_arg=(block_arg : Arg | Nil) #

[View source]
def block_arity : Int32 | Nil #

Number of block arguments accepted by this method. nil if it does not receive a block.


[View source]
def block_arity=(block_arity : Int32 | Nil) #

Number of block arguments accepted by this method. nil if it does not receive a block.


[View source]
def block_nest : Int32 #

[View source]
def block_nest=(block_nest : Int32) #

[View source]
def body : ASTNode #

[View source]
def body=(body : ASTNode) #

[View source]
def c_calling_convention : Bool? #

[View source]
def c_calling_convention=(c_calling_convention : Bool | Nil) #

[View source]
def c_calling_convention? #

Returns self as an External if this Def is an External that must respect the C calling convention.


[View source]
def call_convention #

[View source]
def calls_initialize=(calls_initialize : Bool) #

[View source]
def calls_initialize? : Bool #

[View source]
def calls_previous_def=(calls_previous_def : Bool) #

[View source]
def calls_previous_def? : Bool #

[View source]
def calls_super=(calls_super : Bool) #

[View source]
def calls_super? : Bool #

[View source]
def captured_block=(captured_block : Bool) #

[View source]
def captured_block? : Bool #

[View source]
def clone_without_location #

[View source]
def closure=(closure : Bool) #

[View source]
def closure? : Bool #

[View source]
def doc : String | Nil #
Description copied from class Crystal::ASTNode

Returns the doc comment attached to this node. Not every node supports having doc comments, so by default this returns nil.


[View source]
def doc=(doc : String | Nil) #

[View source]
def double_splat : Arg | Nil #

[View source]
def double_splat=(double_splat : Arg | Nil) #

[View source]
def expand_default_arguments(program, args_size, named_args = nil) #

[View source]
def expand_new_default_arguments(instance_type, args_size, named_args) #

[View source]
def expand_new_from_initialize(instance_type) #

[View source]
def expand_new_signature_from_initialize(instance_type) #

[View source]
def fill_body_from_initialize(instance_type) #

[View source]
def free_var?(node : Path) #

[View source]
def free_var?(any) #

[View source]
def free_vars : Array(String) | Nil #

[View source]
def free_vars=(free_vars : Array(String) | Nil) #

[View source]
def freeze_type : Type | Nil #

[View source]
def freeze_type=(freeze_type : Type | Nil) #

[View source]
def has_any_args? #

[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode) | Nil, block : Crystal::Block | Nil, interpreter : Crystal::MacroInterpreter, name_loc : Location | Nil) #

[View source]
def llvm_intrinsic? #

[View source]
def macro_def=(macro_def : Bool) #

[View source]
def macro_def? : Bool #

[View source]
def macro_owner #

[View source]
def macro_owner=(macro_owner : Crystal::Type | Nil) #

[View source]
def macro_owner? : Crystal::Type? #

[View source]
def mangled_name(program, self_type) #

[View source]
def map_type(type) #

[View source]
def match(objects, &) #

Yields arg, arg_index, object, object_index corresponding to arguments matching the given objects, taking into account this def's splat index.


[View source]
def min_max_args_sizes #

Returns the minimum and maximum number of positional arguments that must be passed to this method, assuming positional parameters are not matched by named arguments.


[View source]
def naked=(naked : Bool) #

true if this def has the @[Naked] annotation


[View source]
def naked? : Bool #

true if this def has the @[Naked] annotation


[View source]
def name : String #

[View source]
def name=(name : String) #

[View source]
def name_location : Location | Nil #

[View source]
def name_location=(name_location : Location | Nil) #

[View source]
def name_size #

[View source]
def new=(new : Bool) #

Is this a .new method that was expanded from an initialize?


[View source]
def new? : Bool #

Is this a .new method that was expanded from an initialize?


[View source]
def next : Def | Nil #

[View source]
def next=(next __arg0 : Def | Nil) #

[View source]
def no_inline=(no_inline : Bool) #

true if this def has the @[NoInline] annotation


[View source]
def no_inline? : Bool #

true if this def has the @[NoInline] annotation


[View source]
def original_name #

[View source]
def original_name=(original_name : String | Nil) #

Name of the original def if this def has been expanded (default arguments)


[View source]
def original_name? : String | Nil #

Name of the original def if this def has been expanded (default arguments)


[View source]
def original_owner : Type #

[View source]
def original_owner=(original_owner : Type) #

[View source]
def original_owner? : Type | Nil #

[View source]
def owner : Type #

[View source]
def owner=(owner : Type) #

[View source]
def owner? : Type | Nil #

[View source]
def pretty_print(pp) : Nil #

[View source]
def previous : DefWithMetadata | Nil #

[View source]
def previous=(previous : DefWithMetadata | Nil) #

[View source]
def proc_c_calling_convention? #

[View source]
def raises=(raises : Bool) #

[View source]
def raises? : Bool #

[View source]
def receiver : ASTNode | Nil #

[View source]
def receiver=(receiver : ASTNode | Nil) #

[View source]
def return_type : ASTNode | Nil #

[View source]
def return_type=(return_type : ASTNode | Nil) #

[View source]
def returns_twice=(returns_twice : Bool) #

true if this def has the @[ReturnsTwice] annotation


[View source]
def returns_twice? : Bool #

true if this def has the @[ReturnsTwice] annotation


[View source]
def self_closured=(self_closured : Bool) #

[View source]
def self_closured? : Bool #

[View source]
def self_restriction_type : Type | Nil #

Used to override the meaning of self in restrictions


[View source]
def self_restriction_type=(self_restriction_type : Type | Nil) #

Used to override the meaning of self in restrictions


[View source]
def short_reference #

[View source]
def special_vars : Set(String) | Nil #

[View source]
def special_vars=(special_vars : Set(String) | Nil) #

[View source]
def splat_index : Int32 | Nil #

[View source]
def splat_index=(splat_index : Int32 | Nil) #

[View source]
def target_cpu : String | Nil #

Set to the value of the @[TargetFeature(cpu: "apple-m4")] annotation


[View source]
def target_cpu=(target_cpu : String | Nil) #

Set to the value of the @[TargetFeature(cpu: "apple-m4")] annotation


[View source]
def target_features : String | Nil #

Set to the value of the @[TargetFeature("+sve,+sve2")] annotation


[View source]
def target_features=(target_features : String | Nil) #

Set to the value of the @[TargetFeature("+sve,+sve2")] annotation


[View source]
def to_json(builder : JSON::Builder) #

[View source]
def uses_block_arg=(uses_block_arg : Bool) #

[View source]
def uses_block_arg? : Bool #

[View source]
def varargs? #

[View source]
def vars : MetaVars | Nil #

[View source]
def vars=(vars : MetaVars | Nil) #

[View source]
def visibility : Crystal::Visibility #

[View source]
def visibility=(visibility : Crystal::Visibility) #

[View source]
def yield_vars : Array(Var) | Nil #

[View source]
def yield_vars=(yield_vars : Array(Var) | Nil) #

[View source]