Code Monkey home page Code Monkey logo

Comments (9)

solnic avatar solnic commented on June 8, 2024 1

bump :)

from dry-types.

backus avatar backus commented on June 8, 2024

I might add a separate issue for this, but I think that Dry::Types::Struct objects should also change their behavior such that attribute values are duplicated upon instantiation:

require 'dry/types'

class Name < Dry::Types::Struct
  attribute :first, 'strict.string'
  attribute :last,  'strict.string'
end

name1 = Name.new(first: 'john', last: 'doe') # => #<Name first="john" last="doe">
name2 = Name.new(name1)                      # => #<Name first="john" last="doe">

name1.equal?(name2) # => false

name1.first.reverse!

name1.first # => "nhoj"
name2.first # => "john"

this way you can still have mutable structs if you want but you still aren't as likely to accidentally mutate the state of another object.

from dry-types.

solnic avatar solnic commented on June 8, 2024

I might add a separate issue for this, but I think that Dry::Types::Struct objects should also change their behavior such that attribute values are duplicated upon instantiation

We can provide a special struct type that does that if it could be useful. But I wouldn't want this to be THE behavior of every struct, because of performance cost.

from dry-types.

solnic avatar solnic commented on June 8, 2024

True, we should deeply freeze its members. It's gonna cost a lot in terms of performance though so it should be properly documented.

from dry-types.

timriley avatar timriley commented on June 8, 2024

Given the performance cost, can you imagine anyone actually wanting to use such a class?

from dry-types.

solnic avatar solnic commented on June 8, 2024

Of course, there are places where being so paranoid is a good idea, esp when you rely heavily on object's equality and you need to make sure nothing can mutate them. I had this kind of code a couple of times, I always used truly immutable objects there.

from dry-types.

backus avatar backus commented on June 8, 2024

It's gonna cost a lot in terms of performance though so it should be properly documented.

Assuming this is properly documented are you comfortable then with moving forward on this? I'm happy to do a PR at some point I just want to make sure that this is inline with what the project's goals

from dry-types.

solnic avatar solnic commented on June 8, 2024

Yes let's do this

from dry-types.

backus avatar backus commented on June 8, 2024

On it. I assume you are fine with pulling in IceNine as a dependency for this?

from dry-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.