Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I'm not sure about Java, but C# has a nifty feature where you can add a constraint that the type used must support a public parameterless constructor.

    class AutoVivifyDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : new() {

        public new TValue this[TKey key] {
            get {
                TValue v;
                if (this.TryGetValue(key, out v)) {
                    return v;
                } else {
                    this.Add(key, (v = new TValue()));
                    return v;
                }
            }
            set {
                base[key] = value;
            }
        }

    }
    I quite like generics but it does seem to involve a lot of headache inducing angle brackets.