-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importing from other files/packages #6
Comments
of course, I can use conditional compilation to achieve this, though it would be nice to have a simpler way. If I had many files it would become tempting to write some kind of pre- or postprocessor that converts magic comments into the language-specific imports or includes. |
What are you trying to achieve? |
Ah I see so it makes a bundle. That's probably fine for my purposes, though if an API gets large enough I imagine there's a benefit to importing/including only the classes one needs. I'm looking to make a For example the |
I see. One problem with namespaces is that every target language does it differently:
Therefore there's even no Ć construct to define the namespace. It is passed on the command line instead. Referencing namespaces or modules is another story. I'm all open for discussion how to solve it best! |
JS has had the CommonJS system for a long time now, where you can Now a newer ES Modules syntax has landed in the latest versions of node/browsers, which looks like So there could be an option to output multiple files and have them import from each other by relative paths, as well as an option whether to output CommonJS or ES Modules syntax. As far as one Ć lib importing from another, it's definitely tricky:
I doubt there would be a problem using the same user-configured name for C++/C# namespaces and Java packages (with the appropriate transformations), but I think we'd want to let the user configure JS and Python packages names separately. |
btw what's the command line option for namespaces you mentioned? |
The Thanks for your comment! This is a complex problem and I'd like to approach it with small steps. Let's start with JS modules. My understanding is that there's a widely used CommonJS and an incompatible emerging ES Modules? |
The Node.js docs cover CommonJS in https://nodejs.org/dist/latest-v12.x/docs/api/modules.html#modules_require_id And ECMAScript modules are covered in https://nodejs.org/dist/latest-v12.x/docs/api/esm.html. There's no hierarchical naming convention like Java packages or nested namespaces in C++/C#, because no one's putting
// ES Modules way
import * as myNamespace from 'my-package' // myPackage published to npm
myNamespace.MyClass
// Or
import { MyClass } from 'myPackage' // CommonJS way
const myNamespace = require('my-package') // myPackage published to npm
myNamespace.MyClass
// Or
const { MyClass } = require('my-package') Or you can import from a local file instead of an npm package by doing import * as myNamespace from './path/to/file' (Same for CommonJS) The file you're importing from must have // ES Modules way
export class MyClass { ... } // CommonJS way
class MyClass { ... }
exports.MyClass = MyClass Packages published to npm have to have a globally unique name and the convention is |
The first blocker for me on this issue is actually just that there's no way to make cito take external .ci files into account without bundling them. For example, in my If cito had a flag to include files for type checking but not output them, then I could at least add the necessary import and export declarations for each language in my own postprocessing. |
Actually I noticed this CLI option:
Almost sounds like what I'm looking for, but I'm not sure, and I couldn't manage to get it to prevent type not found errors anyway. |
Do you want to author two separate packages that have a common part? |
Please pull and you can try:
(
You can try top-level // at the top level of CompassParser.ci
#if ES
native { import { Length, Angle } from "unitized" }
#elif CJS
native { const { Length, Angle } = require("unitized") }
#elif CSHARP
...
#endif |
Yup, and I would use different namespaces if I were writing the C++ etc by hand for the packages anyway. It would be possible to insert the appropriate |
My point is that I'd like to import a different namespace without resorting to conditional compilation. But let's try |
The |
@pfusik ah, agreed, it would be convenient of course to have cito deal with namespace imports. I just assume it will take awhile so we can decide carefully how to let the user configure everything (I think some kind of file to go along with every bundle that configures namespaces for C++/C#, prefix for C, and import paths or packages for JS/Python etc...) |
A "project" file. Cool idea! |
yup exactly. that way when building my It would probably make sense to have a list of source |
All Node.js projects have a |
I consider this an important feature. I'm just still far from knowing how to design it. After so many decades, C and C++ don't have a standard package manager or project file. Ć is on par with them in this area. Suggestions are welcome! |
Yeah it's tough. Whenever I get back to working on this stuff I'll play around with putting in the import statements for each target language with macros. |
Another somewhat related topic is integrating I have enabled https://github.com/pfusik/cito/discussions for general discussions. It might work better than the tracker which is suited to well-defined tasks. |
If I am to add anything, is that I've been developing applications in PHP for over 5 years now, and it's bloody irritanting and unmaintainable mess that everything from PHP is in global namespace. Please, don't make the same mistake in Ćito, and if there are to be namespaces (and I think they must be), then please put everything that Ćito provides into some kind of namespace. Don't leave anything in global namespace. Question. I assume @pfusik you don't want to do dynamic imports? Like in Python, JS and PHP you can do this: def method():
print("unloaded")
from x import y
print("imported ", y) function method() {
console.log("unloaded");
import("x").then(y => console.log("loaded"));
} function method() {
echo "unloaded":
$x = require('./x');
echo "loaded" . $x; Right? Because how would you transpile that to Java/C#/C++, right? No can do (or I am unaware of any such thing). So the only way of any kind of modularity are namespaces, correct? Or am I missing something. |
Could you give some examples? I'm unfamiliar with PHP. Note that C doesn't have namespaces either and it hasn't stopped people from implementing major operating system kernels for the last five decades, several compilers and 3D engines.
What do you mean by "dynamic" ? public void Foo()
{
import Bar.Quux; // transpiles to nothing
Quux.DoSth(); // transpiles to Bar.Quux.DoSth();
} |
Virtually every function that exists in PHP is in global namespace.
Yes, but there is this
No, not true, because in dynamic imports, when there's an exception that exception is thrown at the time of the import. File x = 2
raise Exception("welcome")
y = 3 When you run this code print("hello")
from source import x
print("there", x) you will see hello
Traceback (most recent call last):
File "C:\Users\Daniel\PycharmProjects\sync\test.py", line 2, in <module>
from source import x
File "C:\Users\Daniel\PycharmProjects\sync\source.py", line 2, in <module>
raise Exception("welcoem")
Exception: welcome That's how imports work in Python, JS and vanilla PHP. That's exactly what I meant when I asked #65 And opposite would be how imports work in Java and C#. The C++ as you noticed is an exception. I think that's because you can include only |
The language reference doesn't say anything about importing from one ci file to another, are there any import statements yet?
I'm hoping to be able to use it to make packages that depend on each other. I mostly have experience publishing Node.js packages, but would like to be able to make Maven, Pip, etc. packages (maybe vcpkg for C++, no idea if it's any good...) I know modules differ a lot from one language to another so I wouldn't be surprised if Ć lacks any solution for this yet.
The text was updated successfully, but these errors were encountered: