digitalmars.D.learn - Introspection of exceptions that a function can throw
- Mark (4/4) Feb 24 2021 Is there a way to obtain a list, at compile-time, of all the
- SealabJaster (11/15) Feb 24 2021 I don't believe that there's any built-in way, as that'd mean
- SealabJaster (4/5) Feb 24 2021 Just realised that can include duplicates, but there's
- Dennis (4/7) Feb 24 2021 No, since this is not known at compile-time.
- James Blachly (6/11) Feb 25 2021 Crazy idea:
- Arafel (11/24) Feb 25 2021 You'd hit a very big wall with separate compilation unless you can
Is there a way to obtain a list, at compile-time, of all the exception types that a function might throw (directly or through a call to another function)? Thanks.
Feb 24 2021
On Wednesday, 24 February 2021 at 19:38:53 UTC, Mark wrote:Is there a way to obtain a list, at compile-time, of all the exception types that a function might throw (directly or through a call to another function)? Thanks.I don't believe that there's any built-in way, as that'd mean that functions would have to define what they throw as part of their signature (like in Java). But you can semi-emulate it with metaprogramming, it's just that you'll have to manually keep all the information up to date. Here's an example: https://run.dlang.io/is/UUHumO Which supports saying what a function directly throws (` Throws`), and also saying what functions it calls that may also throw well-defined exceptions (` ThrowsInclude`), and results in `(Exception, MyExcept)`
Feb 24 2021
On Wednesday, 24 February 2021 at 19:58:41 UTC, SealabJaster wrote:...Just realised that can include duplicates, but there's std.meta.NoDuplicates to solve that.
Feb 24 2021
On Wednesday, 24 February 2021 at 19:38:53 UTC, Mark wrote:Is there a way to obtain a list, at compile-time, of all the exception types that a function might throw (directly or through a call to another function)?No, since this is not known at compile-time. See: https://forum.dlang.org/post/qnarroejixxtqxjiwvhy forum.dlang.org
Feb 24 2021
On 2/24/21 2:38 PM, Mark wrote:Is there a way to obtain a list, at compile-time, of all the exception types that a function might throw (directly or through a call to another function)? Thanks.Crazy idea: Could a program import its own source file as a string (`string source = import('thisfile.d')`) and `-J<path>` , then use a lexer/parser to generate AST of the source code and extract exceptions potentially thrown by given functions -- all at compile time?
Feb 25 2021
You'd hit a very big wall with separate compilation unless you can inspect all the code, and know where to find it. But you'd have a problem, for instance, if you are writing a plugin (.so / DLL) for a product for which you only have .di files. Or even worse the other way round: if you want to allow people to write plugins for your product, you can't know what they'll throw, even if they have your code, unless you enforce a `nothrow` interface. But I guess that if you're not doing any of this, it should be possible... although I'd still do it as a separate pre-compilation step, so it could be cached. On 26/2/21 3:21, James Blachly wrote:On 2/24/21 2:38 PM, Mark wrote:Is there a way to obtain a list, at compile-time, of all the exception types that a function might throw (directly or through a call to another function)? Thanks.Crazy idea: Could a program import its own source file as a string (`string source = import('thisfile.d')`) and `-J<path>` , then use a lexer/parser to generate AST of the source code and extract exceptions potentially thrown by given functions -- all at compileĀ time?
Feb 25 2021