GCC 4.6.0 released
Posted Mar 29, 2011 2:39 UTC (Tue) by
HelloWorld (guest, #56129)
In reply to:
GCC 4.6.0 released by cmccabe
Parent article:
GCC 4.6.0 released
The fact that Java needs wrapper classes for this, and for other things, is a design limitation of Java, not of primitives themselves.
The term "Primitive type" refers to different things in different languages. I was talking about primitive types in Java, and they're a bad idea. I don't know about Go and its notion of primitive types.
Having a bool type is "obviously a bad thing," as you can obtain the same effect with ints.
Um no, you can't. The point of having a boolean type is to enable the compiler to check that there is actually a logical value in your if statement or while loop, and not a numeric value.
Oh wait, that argument is bogus.
I said something is obviously a bad thing if it's not needed
and causes bugs. You deliberately didn't quote the second bit, because you knew that if you had, you wouldn't have had much of a point. Please stop doing that. And please tell me: what is in your opinion a more convincing way to show that a language feature is a bad idea than showing that it's dangerous and that you don't actually need it?
This is just more "argument by assertion" on your part. I find NULL, or nil, or None, or whatever you call it, to be useful more often than it is not. And when I don't have it, I usually have to create some kind of special class value to represent "zero" which is usually just an annoyance.
It is trivial to do this once generically. Heck, I'll just do it for you.
abstract class Option<T> {
public abstract T get();
public abstract bool isPresent();
}
final class None<T> extends Option<T> {
public T get() { throw new Ouch(); }
public bool isPresent() { return false; }
}
final class Some<T> extends Option<T> {
private final T t;
public Some(T t) { this.t = t; }
public T get() { return t; }
public bool isPresent() { return true; }
}
Put this in java.util and you have everything you need to represent optional values. If a method takes an optional value of type T you declare a formal parameter of type Option<T>. If you don't want to pass a value, pass new None<T>(), otherwise, pass new Some<T>(myT). What does null do better than this simple class hierarchy?
(
Log in to post comments)