sway icon indicating copy to clipboard operation
sway copied to clipboard

Create an implicit Sway `prelude`

Open emilyaherbert opened this issue 3 years ago • 2 comments

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).

emilyaherbert avatar Feb 25 '22 15:02 emilyaherbert

Related: #330

adlerjohn avatar Feb 28 '22 21:02 adlerjohn

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.

mitchmindtree avatar Aug 11 '22 06:08 mitchmindtree

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.

mohammadfawaz avatar Sep 06 '22 15:09 mohammadfawaz

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

sezna avatar Sep 10 '22 02:09 sezna

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

In this regard it doesnt make sense to follow Rust, but rather Solidity's design, right?

SwayStar123 avatar Sep 10 '22 17:09 SwayStar123

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.

sezna avatar Sep 12 '22 00:09 sezna

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).

mohammadfawaz avatar Sep 12 '22 13:09 mohammadfawaz

Very good

xyx202 avatar Sep 13 '22 12:09 xyx202