In this series of posts I will list the things that I always loved about Ruby. Some of these work differently in other languages or they don't exis...
For further actions, you may consider blocking this person and/or reporting abuse
The thing in Ruby and Crystal is that you can overload the default, if it doesn't work for your data structure. This isn't true for Haskell. But I agree that you can have the opposite: instead of requiring
deriving (Eq, Show)
, take that for granted and let the developer opt out.You seams to be very confident that you can define a meaningful equal for an listish class that can contains meaningful equals for structy stuff that also has meaningful equals that works in the case of cycles.
Are you sure? can you show me code/pseudocode that would work?
The iconic example is the Person with a list of friends that can be Persons
It can be done with a special equals that takes a list of visited or something like that... can you do it with a normal binary equal method? how?
Hi! I can show code that check for recursiveness for
to_s
. For==
it should be similar, but considering a pair of objects.That is, before printing ourselves we remember that we passed through this object. If later we pass again, we don't recurse and just output "[..]".
For equality it would be similar, but putting the two objects in a global table.
You can try to check how this is done in Ruby, thought it's C code so it will be harder to understand: github.com/ruby/ruby/blob/c53bdb8f... (note that it calls a function
recursive_equal
)Hi, This is not really satisfactory:
-what about parallelism?
-what if you want to point to the circular object by giving it a name instead of just printing "repeated stuff here"
For the equality you will need that.
For equality of circular objects you even need problems like DFS minimization:
For example assume you have a Node=Struct.new(int,node)
is it the case that
start=Node(0,start)
is equal to
start=Node(0,Node(0,start))
? in certain interpreations of recursive equality it is the same, in other it is not.
Good points, although I don't think parallelism is relevant here. Of course if you mutate things while inspecting them you get odd results. This is the same for equality or any other operation.
About circular objects, the answer is: structs are passed as values, so they can't be circular by restriction, and a class (reference) would consider the two examples above to be different. If you want something smarter, you have to code it yourself. The motto here could be
``do simply what works most of the time and let the complex be easily incorporated too''
? are you talking about ruby here? Structs are definitively passed by reference.
If they were passed by value then methIn would only be altering its local copy.
I do not think that anything at all is passed by value in ruby.
Sorry! I meant in Crystal.
ok then... I'm interested in the gritty details of circular equality in ruby.
I also wrote on reddit (reddit.com/r/ruby/comments/syemrs/...) searching for a better answer, and I'm getting some, but I'm still confused.
The way it works is:
What happens if these objects are compared in other threads at the same time? Well, the global table keeps these pairs per thread!
At least that's how it will work in Crystal (supporting recursive
to_s
is done with a similar algorithm.)If that default equality isn't a good default, you can always override
==
in Ruby and Crystal.You can also take a look at the way we are probably going to do it in Crystal (similar to Ruby, but you don't need to understand C): github.com/crystal-lang/crystal/is...
That is not entirely true.
Struct#==
also requires type identity, including generic parameters. Which reminded me of this issue: github.com/crystal-lang/crystal/is...