sway
sway copied to clipboard
Create an implicit Sway `prelude`
Per the discussion on #830, we will eventually want to have a Sway prelude akin to the Rust prelude (https://doc.rust-lang.org/std/prelude/index.html).
Related: #330
Adding some quick thoughts on this as it was raised again recently in #2433:
The simplest approach might be to add prelude names while constructing the initial Namespace. The Namespace contains an init module that is cloned when initialising each module within a Sway project. It contains the names that should be implicitly available from every module. Perhaps we can add the prelude names to this init module upon constructing the Namespace with the condition that the std library must be a dependency.
From @SwayStar123's desription in https://github.com/FuelLabs/sway/issues/2433:
Heres some nominations for addition to the prelude
- Address
- ContractId
- Identity
- transfer()
- msg_sender()
- msg_amount()
- msg_asset_id()
- StorageMap
- Vec
- assert
- require
- revert
I will close #2433 in favour of this issue.
Hm, I think a lot of the above can remain scoped. Address, ContractId, and Identity, Vec, assert, require, and revert should probably be included. But msg_sender, transfer, msg_amount, msg_asset_id, and StorageMap can remain scoped IMO. Following Rust's prelude design, there aren't many global functions and all data structures except Vec are in std::collections
Hm, I think a lot of the above can remain scoped. Address, ContractId, and Identity, Vec, assert, require, and revert should probably be included. But
msg_sender,transfer,msg_amount,msg_asset_id, andStorageMapcan remain scoped IMO. Following Rust's prelude design, there aren't many global functions and all data structures exceptVecare instd::collections
In this regard it doesnt make sense to follow Rust, but rather Solidity's design, right?
That's a good question. This is really subjective. Personally, I dislike magical global functions in general, although data structures are less offensive to me. That's purely subjective though, so let's go ask in the community discord what people think.
Personally, I dislike magical global functions in general
Agree 100%. If we implement the following proposal https://github.com/FuelLabs/sway/issues/2463, then a lot of these functions become methods and they will be automatically available by only importing Contract (which itself might be added to the prelude already). We could even consider adding more similar types such as Block which would expose timestamp() and height() methods (instead of the free functions in https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/block.sw).
Very good