Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 8
    > "What are you going to do with "a generic Success type" anyway?" — when writing generic code, a single value type (it's called Unit type btw) is very useful because it eliminates special case of functions that "just return" but don't return anything in particular. But when Java was first created, it has even less expressive type system (without type parameters), so there were not much practical difference. And now it's too late to change. More modern languages actually eschew void "type" (actually it's not even a type, just a special marker for methods) and use Unit type instead. Commented Apr 24, 2017 at 18:10
  • 9
    @SargeBorsch Java's Void type acts as a Unit type (e.g. for Generics), though sadly, it is different from void (side note: a cuddly kitten dies every time you invent a new type to fix the type you messed up in the previous version). If anyone is not familiar with Unit types, this is a decent intro: en.wikipedia.org/wiki/Unit_type Commented Apr 24, 2017 at 19:09
  • 1
    @OgrePsalm33 it doesn't really act as a Unit type (at least in practical means — one still has to write "return null;" to return from method which has Void return type, and AFAIK compiler does allocate space on stack for Void references, not taking advantage of the fact that it's pointless to ever read these values), it's merely a convention to use it where "proper" Unit type would be more appropriate. Commented Apr 24, 2017 at 19:19
  • 3
    @immibis because Unit type by definition must have exactly one value, and Void doesn't have values. Yes, it's possible to use null (in fact this is the only choice), but null is a special thing, any value of reference type in Java may be null (this is another blunder in language design). And, as I said earlier, if you are using Void return type instead of void mark, Java compiler is not your friend. Commented Apr 26, 2017 at 5:15
  • 5
    @SargeBorsch Void has exactly one value, which is null. The fact that null is a member of most type is irrelevant to whether Void is a unit type. Commented Apr 26, 2017 at 5:29