title: Is it acceptable to return null from a factory constructor in Dart?
body: <p>I've been writing some code in Dart.  I really love the factory constructor, but I'm afraid that I'm abusing it's usefulness.  In particular, when I write a value object class, I sometimes return null if the validation fails.</p>
  
  
  
  <pre class="lang-dart prettyprint-override"><code>class EmailAddress {
    static final RegExp _regex = new RegExp(...);
    final String _value;
  
    factory EmailAddress(String input) {
      return _regex.hasMatch(input) ? new EmailAddress._internal(input) : null;
    }
  
    const EmailAddress._internal(this._value);
  
    toString() =&gt; _value;
  }
  </code></pre>
  
  <p>At first, this doesn't seem all that bad.  However, when you actually use it, this is what you see.</p>
  
  <pre class="lang-dart prettyprint-override"><code>methodThatCreatesAnEmailAddress() {
    var emailAddress = new EmailAddress("definitely not an email address");
    ...
  }
  </code></pre>
  
  <p>The argument why this is bad is that a developer coming from another statically typed language, such as Java or C++, would expect <code>emailAddress</code> to always be initialized to a non-null value.  The argument why this is perfectly acceptable is that the constructor is factory and, as such, is allowed to return a <code>null</code> value.</p>
  
  <p>So is this bad practice or taking advantage of a useful feature?</p>