equality

Scala equality with type checking?

一个人想着一个人 提交于 2019-12-04 16:12:09
问题 Is there a uniform method to perform equality with type checking? Unfortunately val objectA:String = "test" val objectB:Int = 2 objectA == objectB the equality operator == doesn't complain if objectB is a Int while objectA is a String. I would need an operator like === that perform type checking as well (and I hope it is uniform to all scala obj). Does such operator exist? 回答1: You need to look at scalaz's === for type-safe equals - it's implemented as type class there. You can also watch

How do I implement an operator for a class nested in a generic struct?

半世苍凉 提交于 2019-12-04 16:00:13
问题 When I nest a class inside a generic struct and try to implement the equality operator, like this: struct Outer<T> { class Inner : Equatable {} } @infix func == <T>(lhs: Outer<T>.Inner, rhs: Outer<T>.Inner) -> Bool { return lhs === rhs } I get the following error when I try to run the project: While emitting IR SIL function @_TFCC4Test5Outer5InnerCU__fMS1_FT_S1_ for 'init' at .../Testing.swift:20:11 <unknown>:0: error: unable to execute command: Segmentation fault: 11 <unknown>:0: error:

refl in agda : explaining congruence property

感情迁移 提交于 2019-12-04 15:09:08
With the following definition of equality, we have refl as constructor data _≡_ {a} {A : Set a} (x : A) : A → Set a where refl : x ≡ x and we can prove that function are congruent on equality cong : ∀ { a b} { A : Set a } { B : Set b } (f : A → B ) {m n} → m ≡ n → f m ≡ f n cong f refl = refl I am not sure I can parse what is going on exactly here. I think we are pattern matching refl on hidden parameters : if we replace the first occurence by refl by another identifier, we get a type error. after pattern matching, I imagine that m and n are the same by the definition of refl. then magic

How to compare Enums in TypeScript

痴心易碎 提交于 2019-12-04 14:57:42
问题 In TypeScript, I want to compare two variables containing enum values. Here's my minimal code example: enum E { A, B } let e1: E = E.A let e2: E = E.B if (e1 === e2) { console.log("equal") } When compiling with tsc (v 2.0.3) I get the following error: TS2365: Operator '===' cannot be applied to types 'E.A' and 'E.B'. Same with == , !== and != . I tried adding the const keyword but that seems to have no effect. The TypeScript spec says the following: 4.19.3 The <, >, <=, >=, ==, !=, ===, and !

Defining different equality types as inductive types in Coq

穿精又带淫゛_ 提交于 2019-12-04 10:41:38
I am trying to define in Coq different types of equalities. During an university course my professor gave us the rules of four different types, as follows (I provide just the links to the rules): Gentzen : https://ibb.co/imQOCF Leibniz : https://ibb.co/n0uBzv Martin-Lof : https://ibb.co/fALZKv Path Induction : https://ibb.co/esZuKv The difference among these four types relies on the type C. I am trying to prove the isomorphism among them. Unfortunately I have some troubles in declaring as inductive types the first and the second, because I cannot find a way to specify the type C. I have a

Implementing IEqualityComparer<T> for comparing arbitrary properties of any class (including anonymous)

时间秒杀一切 提交于 2019-12-04 10:39:47
I am writing this neat class implementing IEqualityComparer, so that I can just pass any anonymous type to it (or actually any type with properties) and it will automatically compare the types by comparing the property values of the types. public class CompareProperty<T> : IEqualityComparer<T> { private Type type; private PropertyInfo propInfo; private string _fieldName; public string fieldName { get; set; } public CompareProperty(string fieldName) { this.fieldName = fieldName; } public bool Equals<T>(T x, T y) { if (this.type == null) { type = x.GetType(); propInfo = type.GetProperty

Should DDD entities compare by reference or by ID?

核能气质少年 提交于 2019-12-04 09:38:47
问题 When I started using DDD, I created Equals() methods in my entities that compared the ID of the entity. So two entity objects with the same ID would be considered equal. At some point I thought about that and found that two entities in different states should not be considered equal, even when they describe the same thing (i.e. have the same ID). So now I use reference equality for my entities. I then stumbled over this answer by Mark Seemann, where he writes Entities are equal if their IDs

How to eliminate duplicate entries within a stream based on a own Equal class

丶灬走出姿态 提交于 2019-12-04 07:32:51
I do have a simialar problem like descripted here . But with two differences first I do use the stream api and second I do have an equals() and hashCode() method already. But within the stream the equalitity of the of Blogs are in this context not the same as defined in the Blog class. Collection<Blog> elements = x.stream() ... // a lot of filter and map stuff .peek(p -> sysout(p)) // a stream of Blog .? // how to remove duplicates - .distinct() doesn't work I do have a class with an equal Method lets call it ContextBlogEqual with the method public boolean equal(Blog a, Blog b); Is there any

LINQ: Use .Except() on collections of different types by making them convertible/comparable?

笑着哭i 提交于 2019-12-04 06:08:04
Given two lists of different types, is it possible to make those types convertible between or comparable to each other (eg with a TypeConverter or similar) so that a LINQ query can compare them? I've seen other similar questions on SO but nothing that points to making the types convertible between each other to solve the problem. Collection Types: public class Data { public int ID { get; set; } } public class ViewModel { private Data _data; public ViewModel(Data data) { _data = data; } } Desired usage: public void DoMerge(ObservableCollection<ViewModel> destination, IEnumerable<Data> data) { /

How do I compare strings in Java?

前提是你 提交于 2019-12-04 05:39:32
问题 This post is a Community Wiki . Edit existing answers to improve this post. It is not currently accepting new answers. I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug. Is == bad? When should it and should it not be used? What's the difference? 回答1: == tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are