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

LEO NEW SYNTAX #10

Open
Elexy101 opened this issue Apr 27, 2024 · 0 comments
Open

LEO NEW SYNTAX #10

Elexy101 opened this issue Apr 27, 2024 · 0 comments

Comments

@Elexy101
Copy link
Collaborator

ENHANCED DEPENDENCY SYSTEM

The enhanced dependency system in the Leo programming language simplifies the development of Leo projects by providing better support for external calls to both on-chain and local programs. Here's a breakdown of the key changes and features:

  • Change in Import Statement: Instead of using import foo.leo, you now use import foo.aleo to import dependencies.
    Attaching Dependencies: Dependencies are attached using the CLI. For example, to pull credits.aleo as a dependency, you would use Usage: leo add --network <NETWORK> <NAME>.
Screencast.from.27-04-2024.01.24.11.webm
  • New Registry Structure: There's a new .aleo/registry structure in the ~/.aleo file system to store .aleo files from the Aleo network.
    Automatic Retrieval of Network Dependencies: The system can automatically retrieve network dependencies and build complicated nested dependency structures, simplifying the management of dependencies in your project.
  • Adding Local Dependencies: You can also add local dependencies. For example, to add a local dependency named foo.aleo at path ../foo, you would use leo add -l ../foo foo.

In your Leo file, you would import these dependencies as usual:
Screenshot from 2024-04-27 00-56-39

READING EXTERNAL MAPPING

The Leo programming language supports reading external mappings using the Mapping::get and Mapping::get_or_use functions to retrieve values from a mapping stored in an external contract. Here's an explanation of each line of code:

  1. Reading a Value from a Mapping:
    let val: u32 = Mapping::get(token.aleo/account, 0u32);
  • Mapping::get: This function is used to read a value from a mapping.
  • token.aleo/account: This is the identifier for the external contract's mapping. It specifies the contract (token.aleo) and the name of the mapping (account).
  • 0u32: This is the key used to look up the value in the mapping. In this case, 0u32 is used as the key.
  • let val: u32: This declares a variable val of type u32 (unsigned 32-bit integer) to store the value retrieved from the mapping.
  1. Reading a Value from a Mapping with a Default Value:
    let val: u32 = Mapping::get_or_use(token.aleo/account, 0u32, 0u32);
  • Mapping::get_or_use: This function is similar to Mapping::get, but it allows you to specify a default value to return if the key is not found in the mapping.
  • token.aleo/account: Same as before, the identifier for the external contract's mapping.
  • 0u32: This is the key used to look up the value in the mapping.
  • 0u32: This is the default value to return if the key is not found in the mapping.
  • let val: u32: This declares a variable val of type u32 to store the value retrieved from the mapping or the default value if the key is not found.

Relaxed Shadowing

In Leo programming, relaxed shadowing allows you to use local names (such as for mappings, structs, records, and functions) that overlap with names of external objects (like mappings, structs, records, and functions from external contracts or libraries). This can help simplify code and make it more readable. Here's an explanation of the provided code:
let bar: hello.aleo/foo = bye.aleo/foo(foo {a: 1u32, b: 1u32});

  • let bar: hello.aleo/foo: This declares a variable bar of type hello.aleo/foo, which is a struct or record type defined in the hello.aleo contract or module.
  • bye.aleo/foo(foo {a: 1u32, b: 1u32}): This is an initialization expression for bar. It calls the foo function from the bye.aleo contract or module with a struct literal as an argument. The struct literal {a: 1u32, b: 1u32} creates a new instance of a struct with fields a and b, both initialized to 1u32.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant