On Sat, 19 Aug 2017 08:05:34 -0700 (PDT) Tong Sun <suntong...@gmail.com> wrote:
> Suggesting C# type syntax like "int*?*" so as to take nil as valid value. Should 'int' type also support a 'NaN' then? Nil (NULL) is *not* a number. Thats why it has its own keyword, one almost universally associated with pointers only. Implicit cast from apples to oranges many other languages allow is a plain evil and go compiler says you so. In Go int is for numbers, not for pointers. Metal stuck type (machine word) for storing pointers as numbers is called uintptr. What you called for has a 'Maybe' name. I would like Go2 to switch entirely from nils to Maybes, but have not too much hope it will. > In C#, just by adding a "?" to the end of the type solves the problem. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/ 'T?' is a syntax sugar for Nullable<T>. It instantiates a struct you can also write in in Go: type Mint struct { // Maybe (or Optional) int. value int validNum bool // NaN/nil at birth } func (t *Mint) IsNil() bool { return !t.validNum } One may also generalize it: type Nillable interface { // Maybes IsNil () bool } Now one who uses above Nillable<something> does know its cost and will not confuse some laguage's syntax sugar for standard library compound types (and calls) with language's builtin. > However, more and more people are using json to transport data, and there > will be times that json may be invalid. I.e., the "int" type has 'null' as > input. Ah, so Go core team should puff, bow then break Go type system asap. Just because some sloppy coder in outer world may produce invalid data and present coder is too lazy to write proper validation for that? -- Wojciech S. Czarnecki << ^oo^ >> OHIR-RIPE -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.