Skip to content

added the std.exception.Descendant template #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

carlor
Copy link

@carlor carlor commented Feb 19, 2012

mixin(std.exception.Descendant!("SampleException")) creates a subclass of Exception, producing this code:

class SampleException : Exception {
    this(string m = "", string f = __FILE__, size_t l = __LINE__, Throwable t = null) {
        super(m, f, l, t);
    }
}

@jmdavis
Copy link
Member

jmdavis commented Feb 19, 2012

I've thought about adding something like this but came to the conclusion that it wasn't worth it for two reasons:

  1. You can't have ddoc on the exception type, so it doesn't end up in your ddoc documentation.
  2. Ideally, exceptions would have more information than this. While it's better to have a derived exception structurally identical to Exception rather than just using Exception, because the type gives you additional information, it's far better for the derived type to have additional information. And you can't do that with a mixin like this. So, arguably, it's promoting bad code.

Also, if you have a mixin like this, you really should have Exception's other constructor as well (which admitedly, a number of Phobos exception types are missing):

 this(string msg, Throwable next, string file = __FILE__, size_t line = __LINE__)
{
    super(msg, file, line, next);
}

And I really don't think that Descendant is a particularly good name either. It doesn't indicate in any way shape or form that it's generating an exception. Something like genStdException would make more sense, but again, I don't really think that this is ultimately a good idea.

@JakobOvrum
Copy link
Contributor

I agree and think a better solution is a template mixin that generates the two standard constructors, which is where the real boilerplate is at. However, I don't think that's possible due to bugs or limitations with template mixins at the moment. Still, even a string mixin like this one which only adds the constructors would probably be more appropriate.

@jmdavis
Copy link
Member

jmdavis commented Feb 25, 2012

As JakobOvrum suggests, if we're going to do this, we should probably create a function which generates a string to mixin in the 2 standard constructors rather than have one to generate the whole exception type. That way, we provide a way to reduce boilerplate code but avoid encouraging people to simply create new exception classes with no new data. Those exceptions have their place, but the mixin would probably be more useful with just the constructors, since it could be also be used at least some of the time in exception types which do include other member variables. It also has the advantage of making it possible to have ddoc comments on the exception itself, since it'll just be the constructors which are being mixed in rather than the whole type.

@dnadlinger
Copy link
Contributor

Since there seems to be no consensus on the issue yet (I'd prefer a mixin template for the standard constructors as well), I'd say we close this request in the meantime, okay?

@jmdavis
Copy link
Member

jmdavis commented May 11, 2012

Agreed.

@jmdavis jmdavis closed this May 11, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants