Learning From Other Languages - Immutability

Learning From Other Languages - Immutability

So many talks are about learning new languages and technology, but what can we bring back from those into Ruby...

D79fc498d7a5b2ce12180890247476f0?s=128

Jon Rowe

March 13, 2018
Tweet

Transcript

  1. Learning from other languages Immutability

  2. None
  3. None
  4. None
  5. Immutability In object-oriented and functional programming, an immutable object is

    an object whose state cannot be modified after it is created.
  6. Mutability This is in contrast to a mutable object, which

    can be modified after it is created.
  7. Why is immutability good?

  8. Why is immutability good? → Inherently thread safe

  9. Why is immutability good? → Inherently thread safe → More

    memory efficient
  10. Why is immutability good? → Inherently thread safe → More

    memory efficient → No side effects
  11. Side effects?

  12. Side effects? In medicine, a side effect is an effect,

    whether therapeutic or adverse, that is secondary to the one intended;
  13. Side effects? In computer science a side effect is when

    a function or expression modifies some state outside its scope.
  14. Side effects? a = "string" def some_method(takes_a_string) takes_a_string.upcase! end some_method(a)

  15. Side effects? a = "string" def some_method(takes_a_string) takes_a_string.upcase! end some_method(a)

    puts a # => "STRING"
  16. Side effects? heros = {turtle: "Michaelenglo"} def mutate(hash) hash[:turtle] =

    "Rafael" end mutate heros # => "Rafael" heros # => {:turtle=>"Rafael"}
  17. Side effects? my.contacts << Person.new # => [#<Person>, #<Person>]

  18. None
  19. Immutability in other languages Elixir - everything is immutable.

  20. Immutability in other languages Ada, C#, C++, Java, Perl, Python,

    JavaScript, Racket, Scala
  21. Immutability in Ruby

  22. Immutability in Ruby → Frozen objects

  23. Immutability in Ruby "my_string".freeze {a: :hash}.freeze any_object.freeze

  24. Immutability in Ruby mutate("my_string".freeze) # => RuntimeError: can't modify frozen

    String {a: :hash}.freeze # => RuntimeError: can't modify frozen Hash any_object.freeze # => RuntimeError: can't modify frozen <Object>
  25. Immutability in Ruby → Frozen objects → Primitives such as

    numbers and symbols
  26. Immutability in Ruby 6 += 2 # SyntaxError: (irb):9: syntax

    error, # unexpected tOP_ASGN, expecting end-of-input
  27. Immutability in Ruby → Frozen objects → Primitives such as

    numbers and symbols → String literals from Ruby 2.3, optionally
  28. Frozen string ruby --enable-frozen-string-literal # frozen_string_literal: true

  29. Frozen string # frozen_string_literal: true "Hello".upcase! `upcase!': can't modify frozen

    String (RuntimeError) x = String.new('Hello') x.upcase! puts x #=> 'HELLO'
  30. Immutability in Ruby → Libararies such as: → ice_nine (deep

    freezing objects) → values immutable structs → immutable-ruby immutable enumerables
  31. Conclusion

  32. Conclusion - Benefits → Immutability helps with thread safety →

    Immutability can save memory usage → Immutability has the potential for cleaner, side effect free code.
  33. Conclusion - Drawbacks → In some cases mutability is more

    performant (e.g. RSpec mutates strings to avoid allocations)
  34. Thanks @jonrowe