guregu / null

reasonable handling of nullable values
BSD 2-Clause "Simplified" License
1.84k stars 238 forks source link

Add XxxEquals methods to package #35

Closed MarkKremer closed 4 years ago

MarkKremer commented 6 years ago

I'm trying to compare nullable types but I couldn't find an existing easy way to do it.

Are you interested in adding support for / accepting a PR that adds functions like the following for all nulltypes?

func StringEquals(a, b null.String) bool {
    return a.Valid == b.Valid && (! a.Valid || a.String == b.String)
}

// StringEquals(null.NewString("foo", false), null.NewString("foo", false)) -> true
// StringEquals(null.NewString("foo", false), null.NewString("bar", false)) -> true
// StringEquals(null.NewString("foo", true), null.NewString("foo", false)) -> false
// StringEquals(null.NewString("foo", true), null.NewString("foo", true)) -> true
// StringEquals(null.NewString("foo", true), null.NewString("bar", true)) -> false

I'm not entirely sure about the workings of the zerotypes (I don't use them). If I got it right, they can just be compared using their string/bool/int/... value so they don't need the extra function, right?

guregu commented 6 years ago

Good idea! How about we add an Equals() method to every type? Might be cleaner than adding new free functions.

guregu commented 4 years ago

Merged in #36.