Patrick,

I think you're making valuable points here. I also can't think of cases where 
you wouldn't also import a module in case it was found to be importable. So the 
use cases I can think of could as well be tackled by allowing expressions such 
as `import Foo.Bar` as compile-time checks within the conditions of `#if` like 
you suggested. That would bring those libraries only visible within the scope 
of that block.

However, there can be cases where you're considering importing more than one 
module, so something like:

    #if import Foo, import Bar
      ...
    #elseif import Baz
      ...
    #endif

should be considered in that design too. And I don't like the fact that it 
would import many modules in one line of code.

However, I don't get your concerns of "whether already imported or not". Isn't 
`import` strictly about bringing identifiers of linked libraries visible in the 
current file and not about linking to libraries in code.

— Pyry

> I guess one issue I can see is it’s used in two different ways:
> - The first use of canImport is used to check whether it can import a module, 
> and then does so, but there’s no requirement for it to do so. Is this the 
> right this to do?
> - The second use of canImport makes no guarantee that the module has been 
> imported, only that it can.
> 
> What if instead `import` could return whether it imported or not, when used 
> with #if? Instead of ‘can import’, you get ‘did just import’ and ‘has 
> imported’.
> 
> 
> import Required // Error if not present, current behaviour
> 
> #if import CoolThing // Skips code block if not present, imports otherwise
>  // Do something with CoolThing module
> #else
>  import AlmostAsCoolThing
> #endif
> 
> and you test at the use-site
> 
> #if module(X) // Does not import, only checks if it has been imported
>  // use things that are available in X
> #else
> 
> 
> As per Pyry’s feedback, you could add a version:
> 
> #if import Frobnication(<1.7.3) // <- Only added version constraint here.
> extension Knob : Frobnicatable { ... }
> #endif
> 
> 
> 
> Just a way to make it less low level.
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to