Swift Vapor and modules; how to distinguish two overlapping 'type symbols' - swift

I am looking to reuase some (iOS) client side code into a sample Vapor serverside project.
The code in question relies on SwiftyJSON functionality, defined as a struct JSON; now this name slot is not 'free', but already used inside a package that Vapor relies upon (the package name is JSON, as well).
While I can point to the JSON thing I want in my former client side code (as App.JSON), the controller that is boilerplated into the code also uses JSON. And apparently putting as I did the SwiftyJSON library files into the App namespace actually overrides the moduleless references to Vapor's JSON.
I tried to refer then to JSON.JSON, but it does not get recognized. JSON is a defined and compiled framework in the project. Should it not define implicitely a module name, as well?
How can I reach Vapor's JSON, then?
Thanks..

As a matter of fact, the Vapor module also exports JSON. So, the following works:
import Vapor
// Introduce a symbol collision
struct JSON {}
// Import from the Vapor module
print(try Vapor.JSON(node: "test"))

Related

How to find unused imports in umbrella header architecture?

I am looking to find unused imports in an umbrella architecture. The imports are used within swift files. These files receive their imports from Objective-C headers so a text based search would not work. I wrote a script that searches for imports and checks if they are used within the class through text. But the issue is that some imports do not have to be explicitly stated. For example, if we have a framework foo that we use by:
import foo
and that framework has
#import <Example/Example.h>
in its own umbrella header foo.h. I have access to Example, without explicitly importing it.
So the issue is that we cannot see if an import is unused because it does not have to be explicitly imported. Is there a way we can detect unused imports?
I created a text based script in python that searches through a code base and checks for imports that are not used. It would go through directories and look for specific imports, and if they are present check if they are actually being used within the class. I expected that this would work without considering the umbrella architecture. But because there are scenarios where imports do not have to be explicitly stated, this did not work.

Expose Swift Package as part of Another Swift Package

I have a set of Swift Packages that I'm writing (ex: CUIExpandableButton), that I'd like to roll up into another Swift Package called CrystalUI. The overall goal is to write a set of packages that get bundled into a single package. However, I want to make it so people can just have one import
import CrystalUI
instead of a series of import statements
import CrystalUI
import CUIExpandableButton
import PreviewKit
...
Is it possible to re-expose an existing library as part of the parent library?
Looks like #_exported Is what I was looking for. Found this article that explains it. It's an unsupported method but it's also used in Alamofire so I think it's safe.

Prefer imported framework over module classes in Swift

I am in the unfortunate situation where the names of a number of the classes in my project conflict with the classes that are imported from a framework. In general, I want to use the class from the framework in almost all cases. I know that I can specify which class to use by using Framework.ClassName or Module.ClassName. However, this would require adding Framework. to a large number of types.
Is there a mechanism to default to using the framework over the current module class. I.e. in the following example can I make sure that ambiguousInstance is of type MyFramework.ClassName by default?
import MyFramework
struct MyStruct {
var frameworkInstance: MyFramework.ClassName
var moduleInstance: MyModule.ClassName
var ambiguousInstance: ClassName
}
Or, failing that, can someone explain to me the logic behind which class is used when the framework or module is not specified. I.e. in the above case, I have seen the compiler infer that ambiguousInstance is MyFramework.ClassName in some instances and MyModule.ClassName in other instances, and cannot figure out why.
Note: I know having names which can conflict with a framework is not good design, the reason I do now is that I am moving a lot of my code from iOS native to Kotlin multiplatform, and so for the migration, I temporarily have two versions of essentially the same model

What is the purpose of a classless dart file?

I was wondering why there are .dart files without classes. Does it have any advantage?
For example:
import 'package:http/http.dart' as http;
You have to reference it with an "as" and then you can use it like this:
response = await http
.post(
url,
headers: header,
body: dataToSend,
)
Please consider me as a basic flutter developer, probably the answer is out there in a website.
Dart libraries can contain both classes and other top-level declarations, unlike, say, Java where classes are the only top-level declarations allowed, and all other declarations are inside classes.
There is nothing magical about a library which happens to export only non-class declarations. It's just a library.
You don't have to import such a library with a prefix (like as http), you can import it directly as well, and then you can reference the imported names without the prefix:
import "package:http/http.dart";
...
response = await post(url, headers: headers, body: dataToSend);
Likewise, libraries with classes can also be imported with a prefix:
import "dart:convert" as convert;
...
Uint8List utf8 = ...;
String result = convert.utf8.decode(utf8);
That allows you to avoid name conflicts, like the utf8 name here.
In fact, the package:http/http.dart library does export classes, like Response or Client.
The only difference here is that a library which is intended to be imported with a prefix can choose to have shorter names for its top-level members without risking conflicts or ambiguities. Because the name will always be used adjacent to the prefix, extra context from the prefix does not need to be in the name exported from the library.
The post above might conflict with other things named post, but http.post makes it explicit what kind of post we are talking about.
Dart allows you to specify an import prefix In your case its as. With as you are giving the imported library or file a name and you use that name to access properties . It's usually done to prevent a library from polluting your namespace or make the code readable .
and we use dart files without classes mostly to declare constants or function that will repeat in our code so we can access it whenever we want without defining it over and over

Play! Framework Templating Engine issues importing long class names

I've got a List of classes I want to send down to a Scala Template in Play! Framework 2.2.3
however I ran into some issues while trying to do so.
The class I want the list to contain is an arbitrary class type that comes from a package outside of my workspace, but not natively from Java. See the picture below.
Note: I do not have a project/Build.scala file.
The above image represents the first line in my scala template, I have tried to use #import as well (#import com.***.***.type._, com.***.***.type.Version, etc) but to no avail.
This is the error message given to me by Play! Framework.
Is there an issue with the namespacing? Everything works fine when using classes located in my workspace.
The Paths are correct, I've double checked that. For reasons I cannot disclose more code in this region, if more information is required please ask for it and I'll edit the post.
The problem is related to package named type. This word is reserved in Scala as language keyword. You need to escape it like this:
#import List[com.your.package.`type`.Version]