Crystal pass variable by reference or value

How to choose how to pass a variable by value or reference using Crystal?

Example: I would like to pass Struct by reference, and not by value (the documentation explains that it is passed by value, and classes are passed by reference).

+4
source share
1 answer

You cannot choose. You just need to keep in mind that an object that is is Valuepassed by value, other objects are passed by reference.

Struct Value . . , Crystal , . :

struct Mutable
  property value

  def initialize(@value : Int32)
  end
end

def change(mutable)
  mutable.value = 2
  mutable
end

mut = Mutable.new 1
mut = change(mut)
mut.value # => 2
+4

Source: https://habr.com/ru/post/1687091/


All Articles