Git Product home page Git Product logo

Comments (6)

philhofer avatar philhofer commented on August 21, 2024

What if we checked for emptiness by comparing against the zero value of the type? For example:

var empty typeName
if z.someField == empty { /* is empty */ }

from msgp.

bradleypeabody avatar bradleypeabody commented on August 21, 2024

The unfortunate issue with this is we don't have a way of knowing if this will compile, since things like byte slices cannot be compared:

package main

type SomeType struct{ A []byte }
type SomeParentType struct{ SomeField SomeType }

func main() {

	var z SomeParentType
	var empty SomeType
	if z.SomeField == empty { /* is empty */
	}

}

Yields the error:

./prog.go:11:5: invalid operation: z.SomeField == empty (struct containing []byte cannot be compared)

In Playground: https://go.dev/play/p/XufWVZe1Fgn

In my case, I'm not sure if every custom type I have is comparable, thus the idea with the interface. If you feel really strongly about the issue I could probably make it work to refactor some things so everything is comparable, but for my case it's certainly less ideal and a fair bit more work. The interface gives a lot of flexibility, and I'm pretty sure the type check is a fast lookup, maybe it would even optimize down to something static - I can do some decompiling if there's a question on the performance.

from msgp.

philhofer avatar philhofer commented on August 21, 2024

The only issue I can see with the interface is that someone somewhere may have already attached an IsEmpty method to a particular type that means something different. So we might want to make the new behavior opt-in somehow just to make sure existing code continues to work exactly as it does now.

from msgp.

bradleypeabody avatar bradleypeabody commented on August 21, 2024

I see what you mean and agree having this behavior be opt-in would be the least likely to break things for other users.

Using struct tags, two options come to mind:

A. SomeField SomeType `msg:"some_field,omitisempty"` ("omitisempty" would act like "omitempty" but with this additional behavior of checking the interface)
B. SomeField SomeType `msg:"some_field,omitempty,isemptyintf"` (where "isemptyintf" is essentially an option to the omitempty tag)

And in either case this would basically just trigger emitting struct comparisons as the interface check instead of ==, and in all other ways would keep the same omitempty behavior. Obviously primitive types like int won't have methods, but if someone decided to do type A int and then uses A as field type, it seems like omitisempty should trigger the isEmpty interface check since it's entirely possible to add an IsEmpty method to type A. I'll have to look more closely at that specific case, but I don't think it has a big impact on the overall design of this option regardless.

My preference would be to lean toward option A as it seems more concise, let me know if you feel differently. I was trying to think if there would be some other appropriate way to trigger this behavior such as a command line flag, but I'm thinking a struct tag is the most apropos.

If you're good with option A, I'll put together a PR.

from msgp.

bradleypeabody avatar bradleypeabody commented on August 21, 2024

Implemented omitisempty tag in PR #326

from msgp.

klauspost avatar klauspost commented on August 21, 2024

Implemented in #334

from msgp.

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.