Proposal Idea : Library (third party) Importable Checking


(Jo Albright) #1

This is a thought from a current problem I am running into (and have ran into in the past).

If you want to write any code using a library, you must have that library imported into the file. Totally cool with this for most cases.

Situation :

Building a third party library that can be used by others to simplify working with server APIs. I want to give the developer an option to use another third party library within mine. However, I don’t want my library to depend on the other library.

Possible Solution :

Add a system that does two things. 1) Checks for a library and if found imports it into file. 2) Checks if library is imported and allows source kit to parse conditional scope.

@importable ThirdPartyLibrary // would check if library is added to project and import it

if #imported(ThirdPartyLibrary) {
    
    // would check if library is imported before source kit parses scope
    
    // if library is not imported
    // scope would remain unparsed
    // scope could be removed as if it were a comment
    
}

Thanks.

Designer . Developer .  Nerd
Jo Albright


(Jacob Bandes-Storch) #2

This seems useful. Concrete syntax–wise, my first thought would be "import?
ThirdPartyLibrary".

···

On Thu, Jan 14, 2016 at 9:22 AM, Jo Albright via swift-evolution < swift-evolution@swift.org> wrote:

This is a thought from a current problem I am running into (and have ran
into in the past).

If you want to write any code using a library, you must have that library
imported into the file. Totally cool with this for most cases.

Situation :

Building a third party library that can be used by others to simplify
working with server APIs. I want to give the developer an option to use
another third party library within mine. However, I don’t want my library
to depend on the other library.

Possible Solution :

Add a system that does two things. 1) Checks for a library and if found
imports it into file. 2) Checks if library is imported and allows source
kit to parse conditional scope.

@importable ThirdPartyLibrary // would check if library is added to
project and import it

if #imported(ThirdPartyLibrary) {

    // would check if library is imported before source kit parses scope

    // if library is not imported
    // scope would remain unparsed
    // scope could be removed as if it were a comment

}

Thanks.

Designer . Developer .  Nerd
Jo Albright

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Joe Groff) #3

Interesting idea. Unless I'm misunderstanding something, it seems to me you could kill both birds with one stone. If we add an 'importable' predicate to the '#if' language, then you can already use that to conditionalize the import:

#if importable(ThirdPartyLibrary)
import ThirdPartyLibrary
/* decls that depend on library */
#endif

-Joe

···

On Jan 14, 2016, at 9:22 AM, Jo Albright via swift-evolution <swift-evolution@swift.org> wrote:

This is a thought from a current problem I am running into (and have ran into in the past).

If you want to write any code using a library, you must have that library imported into the file. Totally cool with this for most cases.

Situation :

Building a third party library that can be used by others to simplify working with server APIs. I want to give the developer an option to use another third party library within mine. However, I don’t want my library to depend on the other library.

Possible Solution :

Add a system that does two things. 1) Checks for a library and if found imports it into file. 2) Checks if library is imported and allows source kit to parse conditional scope.

@importable ThirdPartyLibrary // would check if library is added to project and import it

if #imported(ThirdPartyLibrary) {
    
    // would check if library is imported before source kit parses scope
    
    // if library is not imported
    // scope would remain unparsed
    // scope could be removed as if it were a comment
    
}

Thanks.


(David Sweeris) #4

I would suggest extending the existing @available syntax rather than using #imported. Either way, though, I like it.

···

On Jan 14, 2016, at 09:22, Jo Albright via swift-evolution <swift-evolution@swift.org> wrote:

This is a thought from a current problem I am running into (and have ran into in the past).

If you want to write any code using a library, you must have that library imported into the file. Totally cool with this for most cases.

Situation :

Building a third party library that can be used by others to simplify working with server APIs. I want to give the developer an option to use another third party library within mine. However, I don’t want my library to depend on the other library.

Possible Solution :

Add a system that does two things. 1) Checks for a library and if found imports it into file. 2) Checks if library is imported and allows source kit to parse conditional scope.

@importable ThirdPartyLibrary // would check if library is added to project and import it

if #imported(ThirdPartyLibrary) {
    
    // would check if library is imported before source kit parses scope
    
    // if library is not imported
    // scope would remain unparsed
    // scope could be removed as if it were a comment
    
}

Thanks.

Designer . Developer .  Nerd
Jo Albright

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jo Albright) #5

Interesting idea. Unless I'm misunderstanding something, it seems to me you could kill both birds with one stone. If we add an 'importable' predicate to the '#if' language, then you can already use that to conditionalize the import:

#if importable(ThirdPartyLibrary)
import ThirdPartyLibrary
/* decls that depend on library */
#endif

-Joe

I am totally on board with that idea… since you could declare a type outside of the “#if importable” scope and add an extension inside the scope.

Designer . Developer .  Nerd
Jo Albright


(Félix Cloutier) #6

If it's meant to be included in a library, don't we need a linkage mechanism that will expose these symbols only if another library is linked in?

Félix

···

Le 15 janv. 2016 à 01:43:16, Jo Albright via swift-evolution <swift-evolution@swift.org> a écrit :

Interesting idea. Unless I'm misunderstanding something, it seems to me you could kill both birds with one stone. If we add an 'importable' predicate to the '#if' language, then you can already use that to conditionalize the import:

#if importable(ThirdPartyLibrary)
import ThirdPartyLibrary
/* decls that depend on library */
#endif

-Joe

I am totally on board with that idea… since you could declare a type outside of the “#if importable” scope and add an extension inside the scope.

Designer . Developer .  Nerd
Jo Albright

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Joe Groff) #7

If it's meant to be included in a library, don't we need a linkage mechanism that will expose these symbols only if another library is linked in?

I see, that's the part I missed. If you wanted these symbols to be conditionally available, that would indeed require a new language, and probably dynamic linker, feature. You could however manage this without any new features, by adding the extensions in a third library dependent on the two base libraries.

-Joe

···

On Jan 14, 2016, at 11:17 PM, Félix Cloutier <felixcca@yahoo.ca> wrote:

Félix

Le 15 janv. 2016 à 01:43:16, Jo Albright via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :

Interesting idea. Unless I'm misunderstanding something, it seems to me you could kill both birds with one stone. If we add an 'importable' predicate to the '#if' language, then you can already use that to conditionalize the import:

#if importable(ThirdPartyLibrary)
import ThirdPartyLibrary
/* decls that depend on library */
#endif

-Joe

I am totally on board with that idea… since you could declare a type outside of the “#if importable” scope and add an extension inside the scope.

Designer . Developer .  Nerd
Jo Albright

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jo Albright) #8

Sad news. Thanks for looking into it.

Designer . Developer .  Nerd
Jo Albright

···

On Jan 15, 2016, at 2:46 PM, Joe Groff <jgroff@apple.com> wrote:

On Jan 14, 2016, at 11:17 PM, Félix Cloutier <felixcca@yahoo.ca <mailto:felixcca@yahoo.ca>> wrote:

If it's meant to be included in a library, don't we need a linkage mechanism that will expose these symbols only if another library is linked in?

I see, that's the part I missed. If you wanted these symbols to be conditionally available, that would indeed require a new language, and probably dynamic linker, feature. You could however manage this without any new features, by adding the extensions in a third library dependent on the two base libraries.

-Joe

Félix

Le 15 janv. 2016 à 01:43:16, Jo Albright via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :

Interesting idea. Unless I'm misunderstanding something, it seems to me you could kill both birds with one stone. If we add an 'importable' predicate to the '#if' language, then you can already use that to conditionalize the import:

#if importable(ThirdPartyLibrary)
import ThirdPartyLibrary
/* decls that depend on library */
#endif

-Joe

I am totally on board with that idea… since you could declare a type outside of the “#if importable” scope and add an extension inside the scope.

Designer . Developer .  Nerd
Jo Albright

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution