3 ms·
That would work. It would need to be a pointer to get a nil pointer exception, or an interface (obv function call to trigger instead of field access), but there
by burnished 2y ago
That would work. It would need to be a pointer to get a nil pointer exception, or an interface (obv function call to trigger instead of field access), but there are some simple and common patterns you would follow to prevent that.
Edit: should do more than just suggest the fix is easy
So for pointer types you have two options IMO: nil checks, or getters that do the nil check for you.
I think a map type instead of a string would also make your point more strongly because your zero value will fail on access. Which yeah that sucks to run into, and I'd agree actually that a lot of Go painpoints pop up early. I appreciate that now, because most of my foe stubbing was done inside the first few weeks, but it is undeniable imo.
- deleted 2y ago[deleted]
- ilrwbwrkhv 2y ago> nil checks, or getters that do the nil check for you Yes I agree there are patterns to solve for this but it is just strange that the compiler doesn't stop you from making these errors. I mean even Typescript has: interface Human { name: string cat?: Cat }
- burnished 2y agoI don't know what 'even typescript' is supposed to mean. Are you saying that typescript is the natural bar for comparison here? That doesn't seem correct, isn't typescript a project that tries to correct javascript's lack of typing? Since it isn't an error to have a nillable type, and it isn't an error to access a field, it makes sense that the compiler doesn't error when you do that thing. If you want a warning when doing something silly then you can setup a linter. If your first thought is that is not very beginner friendly then I agree with you - I edited my original response. But otherwise there are solutions to these problems. If that doesn't suit you and Go isn't sweet enough for you, well, fair. Its rather austere. I don't think it is a categorical error for the project (though there are features I would also like for asking).
- ilrwbwrkhv 2y agoMy point is nillable values by default is a strange choice and throws out 50 years of computing history. It has been clearly established that it leads to null pointer exceptions. The fact that a modern language lets you do it is a bit strange.
- burnished 2y agoOh I didn't really get that from what you had been saying. Sure, I'd agree it feels like a whoopsie, I dislike nil values because they are a common source of faults.