Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well, in a language with nullable reference types, you could use something like

  fn find<T>(self: List<T>) -> (T, bool)
to express what you want.

But exactly like Go's error handling via (fake) unnamed tuple, it's very much error-prone (and return value might contain absurd values like `(someInstanceOfT, false)`). So yeah, I also prefer language w/ ADT which solves it via sum-type rather than being stuck with product-type forever.



How does this work if it is given an empty list as a parameter?

I guess if one is always able to construct default values of T then this is not a problem.


> I guess if one is always able to construct default values of T then this is not a problem.

this is how go handles it;

  func do_thing(val string) (string, error)
is expected to return `"", errors.New("invalid state")` which... sucks for performance and for actually coding.




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

Search: