2 ms·
As others have pointed out, I'm not talking about nil as the zero-valued pointer at the hardware level. I'm talking about nil as the additional member of every
by nostrademons 13y ago
As others have pointed out, I'm not talking about nil as the zero-valued pointer at the hardware level. I'm talking about nil as the additional member of every type in the language's typesystem. A typesystem is an abstraction over the hardware, designed to catch programmer errors and facilitate communication among programmers. There's no reason it has to admit every possible value that the hardware can support.
And people are applying that sort of discipline - see the NullObject pattern in any OO language, or the @Nullable/@NotNull annotations in Java, or !Object types in Google Closure Compiler. The thing is, they have to apply it manually to every type, because the type system assumes the default is nullable. That makes it an inconvenience, which makes a number of programmers not bother.
I'll agree that null pointers are comparatively easy to track down compared to memory corruption caused by multi-threading. Threads are a broken programming model too, and if you want a sane life working with them you'll apply a higher-level abstraction like CSP, producer-consumer queues, SEDA, data-dependency graphs, or transactions on top of them.
- dietrichepp 13y ago> Threads are a broken programming model too More specifically, threads with shared mutable state. If state is never simultaneously shared and mutable then a host of problems disappear.
- nostrademons 13y agoAnd even more specifically - threads with locks. Shared mutable state is okay as long as any state-swapping operations are atomic, eg. with STM or if you build up the state in one thread, switch it with an atomic pointer swap, and don't let other threads mutate the state.
- pcwalton 13y agoThreads with locks are fine as long as the compiler enforces that you take the lock before mutating the data (e.g. Rust). :)
- nostrademons 13y agoAnnotalysis can do this for C++ as well. The problem is that you need to enforce an ordering on locks to avoid deadlocks. That works fine if they're all within one module. It doesn't work at all if you have to coordinate callbacks across threads from different third-party libraries. The usual solution I've seen given for this is "Don't invoke callbacks when holding a lock." This is not a viable solution for most programs.
- twic 13y ago> Threads are a broken programming model too Oh come off it. Threads are in no sense 'broken' - compared to CSP or actors, they just give you a larger set of things you can write. Some of those things are bugs. Others are very useful. For example, a disruptor: http://lmax-exchange.github.io/disruptor/ http://lmax-exchange.github.io/disruptor/ Nulls are broken because they let you write bugs, but don't let you write anything you couldn't write with options or whatever.
- waps 13y agoReally ? How would you even encode Maybe or Option in Java, if you can't use null anywhere ? The problem is that Maybe doesn't work without Algebraic Data Types.
- anon4 13y agoOh come on, that's trivial public interface Maybe<T> { boolean hasValue(); } public final class Just<T> implements Maybe<T> { public final T value; public Just(T value) { this.value = value; } public boolean hasValue() { return true; } } public final class Nothing<T> implements Maybe<T> { public boolean hasValue() { return false; } } You can even get rid of hasValue (but then you need to pay for instanceof each time); or of the Nothing class and make Maybe a class. You may ask what the value of Just.value is before the constructor runs - the value is a machine null and if you somehow manage to access it before the ctor runs, that's a NullPointerException; or what writing "Type varname;" in a function would do - that would be perfectly legal, but you won't be allowed to use it if the compiler can't prove you've initialised it first (which it does right now).
- waps 13y agoProblems : 1) How do you get at the value itself ? (Casting ? That's bad) 2) How do you prevent in Maybe<Integer> x; x == null ? 3) How do you prevent someone from extending Maybe<T> ? e.g. public final class LetsHaveFun<T> implements Maybe<T> { public boolean hasValue() { throw Exception("Can't touch this"); } 4) (you need a null check in the constructor) 5) (I dislike the autoboxing this uses)