Skip to content
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

[Feature] Importing Amber variables #671

Open
Thesola10 opened this issue Feb 7, 2025 · 1 comment
Open

[Feature] Importing Amber variables #671

Thesola10 opened this issue Feb 7, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@Thesola10
Copy link

Thesola10 commented Feb 7, 2025

Is your feature request related to a problem? Please describe.
Currently, the only way to share values between Amber files (i.e. the global scope) is to declare an environment variable. This has the disadvantage of hindered discoverability: There is now a shared assumption being the name of the variable, which escapes compiler analysis.

Describe the solution you'd like
The ability to specify pub let ..., which makes the Amber variable into a singleton, which can be imported into any other file that requires it.

The technical aspect would likely involve reusing the same internal name across importers of the variable in the compiled script.

Describe alternatives you've considered
As above, an environment variable works, but the lack of compiler analysis makes it a possible avenue for issues.

An alternative is to export a getter/setter for the scoped variable, but it is unclear whether successive imports of a file preserve the required scope.

Fully global variables are a possible solution, but the module relationship isn't explicitly stated as it would with an import.

Additional context
This is what I'm currently working on. This suggestion would replace instances of env_var_get(), in src/cli.ab for instance, allowing it to import values loaded in src/main.ab

@Thesola10 Thesola10 added the enhancement New feature or request label Feb 7, 2025
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Feb 7, 2025
@b1ek
Copy link
Member

b1ek commented Feb 10, 2025

i like the idea of exporting a variable, with this syntax which i think is what you meant too:

// file1.ab
pub let word = "Amber";

// file2.ab
import { word } from "./file1";
echo word // stdout: Amber

the compiler limitations concerns are valid, but i wouldn't focus on them too much until an implementation is in the works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants