0

I try to find out what does requires static means and when should I use it.

Short answer is: A requires static clause expresses a dependency that is optional at run time. Frankly speaking I don't understand practical use of it. This is the crux of the matter

I found following explanation: quote from article:

Sometimes we write code that references another module, but that users of our library will never want to use.

For instance, we might write a utility function that pretty-prints our internal state when another logging module is present. But, not every consumer of our library will want this functionality, and they don’t want to include an extra logging library.

In these cases, we want to use an optional dependency. By using the requires static directive, we create a compile-time-only dependency:

module my.module {
    requires static module.name;
}

But it is absolutely unclear for me. Could anyone explain it in a simple way?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • The use case I can think of is that one writes a common library with all different kinds of utility methods, which require lots of third party dependencies. And then the consumer of your library has to provide those dependency they want to use. That way you as a library designer don't have to ship 20+ dependency with your own library. – Lino May 17 '19 at 11:36
  • @sschuberth I decided to create separated topic because the previous one was marked duplicate by mistake – gstackoverflow May 17 '19 at 11:47
  • 1
    That duplicate question has exactly the practical examples you asked for ;-) Had hoped for a quick upvote on a simple answer, but hey, so lets give the upvote to you instead ... and the people on the DUP ... – GhostCat May 17 '19 at 11:48
  • @GhostCat, I agree BUT I want understand example from article I mwantioned in the topic ! – gstackoverflow May 17 '19 at 11:49
  • 3
    Read it again, slowly: **when** that logging module is *present* your code does some logging! In other words: your code senses the presence of that logging module. If it is there, it logs, otherwise it does not. So normal users of your module dont need or want that 3rd party module. But you *need* it to compile. – GhostCat May 17 '19 at 11:50
  • @Lino, ok. I write a library and I want to pack it into module. It depends on 20+ another libraries. What does it mean if I don't want to ship 20+ dependencies. It just won't work – gstackoverflow May 17 '19 at 12:08
  • @GhostCat but how can my code check presence of another module? – gstackoverflow May 17 '19 at 12:19
  • 1
    For example like this? In these cases, we want to use an optional dependency. By using the requires static directive, we create a compile-time-only dependency ... and I guess from there you *could* work your way using reflection or so. – GhostCat May 17 '19 at 13:05
  • @GhostCat, what the reason to have module during compile time if it is supposed to use reflection which works in runtime? – gstackoverflow May 17 '19 at 13:12

0 Answers0