$30 off During Our Annual Pro Sale. View Details »

Learning From Other Languages - Immutability

Jon Rowe
March 13, 2018

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...

Jon Rowe

March 13, 2018
Tweet

More Decks by Jon Rowe

Other Decks in Technology

Transcript

  1. Learning from other languages
    Immutability

    View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. Immutability
    In object-oriented and functional programming,
    an immutable object is an object whose state cannot
    be modified after it is created.

    View Slide

  6. Mutability
    This is in contrast to a mutable object, which can be
    modified after it is created.

    View Slide

  7. Why is
    immutability
    good?

    View Slide

  8. Why is immutability good?
    → Inherently thread safe

    View Slide

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

    View Slide

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

    View Slide

  11. Side effects?

    View Slide

  12. Side effects?
    In medicine, a side effect is an effect, whether
    therapeutic or adverse, that is secondary to the one
    intended;

    View Slide

  13. Side effects?
    In computer science a side effect is when a function
    or expression modifies some state outside its scope.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. Side effects?
    my.contacts << Person.new
    # => [#, #]

    View Slide

  18. View Slide

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

    View Slide

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

    View Slide

  21. Immutability
    in Ruby

    View Slide

  22. Immutability in Ruby
    → Frozen objects

    View Slide

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

    View Slide

  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

    View Slide

  25. Immutability in Ruby
    → Frozen objects
    → Primitives such as numbers and symbols

    View Slide

  26. Immutability in Ruby
    6 += 2
    # SyntaxError: (irb):9: syntax error,
    # unexpected tOP_ASGN, expecting end-of-input

    View Slide

  27. Immutability in Ruby
    → Frozen objects
    → Primitives such as numbers and symbols
    → String literals from Ruby 2.3, optionally

    View Slide

  28. Frozen string
    ruby --enable-frozen-string-literal
    # frozen_string_literal: true

    View Slide

  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'

    View Slide

  30. Immutability in Ruby
    → Libararies such as:
    → ice_nine (deep freezing objects)
    → values immutable structs
    → immutable-ruby immutable enumerables

    View Slide

  31. Conclusion

    View Slide

  32. Conclusion - Benefits
    → Immutability helps with thread safety
    → Immutability can save memory usage
    → Immutability has the potential for cleaner, side
    effect free code.

    View Slide

  33. Conclusion - Drawbacks
    → In some cases mutability is more performant (e.g.
    RSpec mutates strings to avoid allocations)

    View Slide

  34. Thanks
    @jonrowe

    View Slide