(no title)
wrcwill | 2 years ago
from what i understand, pointers become used for that purpose instead (in addition to mutability). which then means that when you get a pointer you don’t know for what reason it is used, so you have to check it even if it was checked higher up in the callstack already.
i wouldn’t actually mind the fact that you can forget to check for nil, i can accept a panic here and there, pretty simple to fix..
the issue i am finding is that it "pollutes" the code, since you end up doing the check more than once for the same value.
if atleast you could only check once and then the compiler would know (ie optionals, or narrowing like TS). ideally you would dereference after the check, but you can’t because pointers are used for mutability AND optionals
in other words, are there any tricks so that you only have to check once?
liampulles|2 years ago
Other than that though, I would say pointers are a last resort for indicating optional (I certainly do use them for this, but uncommonly). Idiomatic Go I think would suggest you use the empty value (I would use an empty value plus an // Optional comment).
Using pointers in general is fairly uncommon in the Go code I write - I don't often need the mutability as you say, because I try to stick to pure functions.
My thinking also, regardless of the language: be suspicious of a function that accepts an optional type. One should ask: is there a non short-circuit or non-error path in the empty case? If not, don't pass an optional, unwrap up the chain. Even if there is a valid path for an empty value, perhaps ask if the function should be split into two cleaner functions.
janderland|2 years ago
wrcwill|2 years ago
Say i have `*Thing`. Are you saying i should cast it to a `Thing`? That is what I was doing initially (im fine with copies), but then you can't call functions that mutate.
leosanchez|2 years ago
coldtea|2 years ago
Perhaps they're used to use, as most mainstream languages didn't use to have it and many still don't (or just have it as a seldom used afterthought, like Java)?
klabb3|2 years ago
The idiomatic way is to return an error or an `ok` boolean at the function boundary. Callers always check return value anyway, this is hard to forget because it’s almost always needed anyway.
The type itself can be value or pointer – its an orthogonal choice. If you have a value type that you return, then you should return the zero value (for instance `time.Time{}`. Both nil and the zero type are comparable by equality check.
This is not ideal, and gets messy when the zero type is a valid value for instance. Generics offer some hope but they are not as capable as in eg Rust.
wrcwill|2 years ago
4death4|2 years ago
realharo|2 years ago
Without it, you don't have that "this is guaranteed to exist at this point" indication that can actually reduce the overall number of checks in practice.