These are chat archives for dry-rb/chat

1st
Dec 2017
Markus Unterwaditzer
@untitaker
Dec 01 2017 12:39
If I have a field of type Array inside a dry struct, I can still modify the array in-place. Wouldn't it make sense to auto-freeze arrays and hashes during struct instantiation?
perhaps something like dup.freeze is actually necessary, since freeze itself is a form of mutation.
Nikita Shilnikov
@flash-gordon
Dec 01 2017 12:46
you can use Dry::Struct::Value I think
but keep in mind that deep-freeze is dead slow
Markus Unterwaditzer
@untitaker
Dec 01 2017 12:48
I'm not even talking about deep-freeze. I have a model Model::Foo where one field is of type Types::Strict::Array.member(Model::Bar). My problem is that that field is still mutable, I can append more Bars.
both models are structs
Nikita Shilnikov
@flash-gordon
Dec 01 2017 12:50
it’s no difference with other types of attributes, say, you have a string attr and you still can mutate it with gsub! and etc
you can workaround this with your own constructor types
Markus Unterwaditzer
@untitaker
Dec 01 2017 12:51
sure, but I wonder if you would accept a PR which would call freeze in Types::Strict::Array or something like that. Or is dry-types not supposed to be always immutable?
Nikita Shilnikov
@flash-gordon
Dec 01 2017 12:52
dry-types has nothing to do with this
though it can have types that dups and freezes values
list of types I mean
like strict but frozen of something
/cc @solnic
Gustavo Caso
@GustavoCaso
Dec 01 2017 12:53
Something in the lines of Types::Freeze::Array
Markus Unterwaditzer
@untitaker
Dec 01 2017 12:54
What do you think about having Types::Strict::Array.frozen as constructor?
I'm wishing for something that would automatically load that constructor for me in any dry-struct (since I want everything in my models to be immutable without me explicitly specifying it for each attribute)
Also not sure what to do with coercible types etc... you'd need Types::CoercibleFrozen, Types::StrictFrozen, ...
Nikita Shilnikov
@flash-gordon
Dec 01 2017 12:57
you probably want to benchmark it. Anyway, we won’t add a new method to the API because it affects everything
you can just redefine Struct.attribute to make it work the way you want
Markus Unterwaditzer
@untitaker
Dec 01 2017 13:03
This message was deleted
isn't the entire point of using dry-struct to have everything as immutable?
Gustavo Caso
@GustavoCaso
Dec 01 2017 13:08
I think using dry-struct is to have something like a value object
Or application data if you will
Represent something from your system
But I’m sure @flash-gordon will be able to provide a better answer
Markus Unterwaditzer
@untitaker
Dec 01 2017 13:09
well if that's all there is to it I might as well keep on using activemodel ;)
Nikita Shilnikov
@flash-gordon
Dec 01 2017 13:11
you certainly can
dry-struct by itself doesn’t provide an interface for mutating attributes
as in, setters
but it doesn’t do anything with values
Markus Unterwaditzer
@untitaker
Dec 01 2017 13:12
I was just being facetious. My point is that I am using dry-struct not because it doesn't support mutation, but because it prevents it
Nikita Shilnikov
@flash-gordon
Dec 01 2017 13:12
it’s not the point
Markus Unterwaditzer
@untitaker
Dec 01 2017 13:12
or at least I thought it did
Nikita Shilnikov
@flash-gordon
Dec 01 2017 13:13
you don’t need a mutable interface, that’s why you don’t get it
Markus Unterwaditzer
@untitaker
Dec 01 2017 13:13
right, I'm not asking for a mutable interface
Nikita Shilnikov
@flash-gordon
Dec 01 2017 13:14
but if you're using other ruby primitives, such as arrays and strings, they're mutable
but since they're more efficient we stick to them
otherwise we would have to go with our own
or freezing everything which is, as I mentioned, is slow
so what we do instead is discouraging
from mutating values
Markus Unterwaditzer
@untitaker
Dec 01 2017 13:16
I'll figure something out. But thanks.
Nikita Shilnikov
@flash-gordon
Dec 01 2017 13:16
and if you want to enforce immutability it will cost you some performance but can do it
Markus Unterwaditzer
@untitaker
Dec 01 2017 13:16
I might have underestimated runtime cost of freeze. I thought it could be O(1)
Nikita Shilnikov
@flash-gordon
Dec 01 2017 13:17
I personally only watched a talk by @solnic where he measured this
tldr we just don't mutate anything thus don't have a problem with mutation :)
Piotr Solnica
@solnic
Dec 01 2017 13:35
it is too slow, deep freezing kills perf. Do it only with memoized objects that are instantiates once per process