goatlang / goat

Extended flavor of the Go programming language, aiming for increased value safety and maintainability
MIT License
63 stars 1 forks source link

"zero value" keyword #11

Open PaperPrototype opened 1 year ago

PaperPrototype commented 1 year ago

In Rust none is the default value. Since golang automatically zero intializes all values it would be nice to check if a value is zero initialized

avivcarmis commented 1 year ago

it would be nice to check if a value is zero initialized Do you mean compare to zero value? you can always do this:

func isZero[T any](v T) bool {
var zero T
return v == zero
}

right? or do you refer to other use cases? Can you provide an example of usage of none?

PaperPrototype commented 1 year ago

it would be nice to check if a value is zero initialized Do you mean compare to zero value? you can always do this:

func isZero[T any](v T) bool {
  var zero T
  return v == zero
}

right? or do you refer to other use cases? Can you provide an example of usage of none?

@avivcarmis Hey, sorry I took so long to reply. Thanks so much for the open mindedness of your reply.

Basically:

var err error
if err == none {
    log.Println("no error");
}
avivcarmis commented 1 year ago

I'm not sure about this one, i'll explain why: you should always be able to tell the zero value of things without the none value. In a non-generic case (like the one you provided here ^) the zero value is clear from the type. In this case it will be nil - the zero value of error. In a generic case you can always write/use the isZero func I added above. Given the this operator does really open up any new capabilities and adds a bit of fragmentation (there will always be more than one way to test for zero values), I'm not sure about it.