Live data from Hacker News

Traps to Developers

qouteall.fun

111–113 of 113 posts

Re: Traps to Developers

#111
post #105

Earlier quoted context omitted.

In any context where you're combining two things that have different kinds of absence. E.g. if you have a cache around an expensive API call, you want to be able to cache null results from that API call, so you need a distinction between "not in the cache" and "cache entry where the API returned null".

Java for example has Map.computeIfAbsent. This function returns a nullable value and does not need 2 layers of if absence to work. I would personally argue that exposing the 2 levels of absence is exposing an implementation details, but I'll accept this as a valid usage of double optionals.

computeIfAbsent doesn't need a second kind of absence because it can never return absence. That's fine if that API is a good fit for your use case, but it isn't always (there's a reason Map.get exists as well as Map.computeIfAbsent).

> I would personally argue that exposing the 2 levels of absence is exposing an implementation details

Probably you wouldn't want to return Option> (or Nullable) to outside callers - maybe you want to convert None to one kind of domain-meaningful error and Some(None) to a different kind of domain-meaningful error, maybe you want to take some different codepaths to respond to "recover" from the different kinds of absence.

But it's extremely valuable to be able to compose together existing libraries that might use absence to mean something and have them just do the right thing rather than always having to worry about the edge cases where one has a kind of absence that's subtly different from the other's kind of absence. I mean fundamentally you can't ever assume that a random third-party function in Java is safe to call with null, because many of them aren't. But you also can't ever assume that a random third-party function won't return null, because some of them do. So even to just compose two functions you've got to check their docs and think about the behaviour of this special value, and it's just all so avoidable.

Re: Traps to Developers

#112
post #37

> A method that returns Optional may return null. projects that do this drive me bananas If I had the emotional energy, I'd open a JEP for a new @java.lang.NonNullReference and any type annotated with it would be a compiler error to assign null to it public interface Alpha {} @java.lang.NonNullReference public interface Beta {} Alpha a = null; // ok Beta b = null; // compiler error javac will tolerate this Beta b; if…

In Kotlin this would already be a compile error, no need for another annotation.

Yeah, sure, and thankfully everyone has already switched all their teams to superior languages

Anyway, I believe what you're referring to is the "?" syntax that annotates types in Kotlin but doesn't help the resulting bytecode, which means that every single library ever would need to convert to kotlin to benefit

  fun doit() : java.io.InputStream? { return null }
  kotlinc test.kt
  javap -c test.class
  public final java.io.InputStream doit();
    Code:
       0: aconst_null
       1: areturn
So even they didn't have the courtesy of marking the result of a known Optional result as Optional when interfacing with the existing Java ecosystem

Re: Traps to Developers

#113

Adjacent to the bit about 100vh, I’d add an item about the much older fundamental stupidity of viewport units, which Firefox tried to fix but no one else got on board and so they eventually removed their patch and went back to being as broken as everyone else. That is: they ignore scrollbars. Use a platform with 17px-wide scrollbars, and in a document with vertical scrolling, 100vw is now 100% of the viewport width……

Thanks for your suggestions.
Post reply on HN