Hacker News new | past | comments | ask | show | jobs | submit login

Here's a four-line replacement for the various `sql.Null*` types in the standard library.

    type Null(type T) struct {
        Val   T
        Valid bool // Valid is true if Val is not NULL
    }
Here's what it looks like in practice: https://go2goplay.golang.org/p/Qj8MqYWWAc3



neat example, thanks. It gives you a generic Null type that has a bool is-valid field. Very similar to the sql.Null{String,Int} types, but you don't have to declare each one. Kinda the point of generics :)


Nice. But I need to retrain myself to read Go code again, because

  func Make(type T)(v T) Null(T) {
  }
looks really confusing and unreadable to me - too many brackets...


I agree. I assume they avoided the standard <> brackets to keep things simple, but really it just makes it harder to parse visually.


I consider the parens () syntax much more readable than the brackets < >


Well, you can try to craft a generic function that returns a function that takes T and returns something of type T, and see how many () you gotta get...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: