Skip to content

Commit da50ed3

Browse files
committed
Add section to documentation on constructors
1 parent b1e1723 commit da50ed3

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/targets/stylus.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,48 @@ The first is the Stylus version the program was activated against; the second is
3737
Note that if the program was already activated, the call to ``activateProgram`` will revert.
3838
Thus, one should structure their program to account for this possibility.
3939

40+
Constructors
41+
____________
42+
43+
For the reason just given, constructors do not work in Stylus like they do in conventional Solidity.
44+
That is, one cannot simply create a contract and expect its constructor to be called.
45+
Instead, one must create the contract, activate it, and then call a function to simulate the contract's construction.
46+
47+
The following is an example. In conventional Solidity, the following would be a perfectly reasonable program:
48+
49+
.. code-block:: solidity
50+
51+
contract C {
52+
uint256 x;
53+
54+
constructor(uint256 _x) {
55+
x = _x;
56+
}
57+
58+
function get_the_number() public view returns (uint256) {
59+
return x;
60+
}
61+
}
62+
63+
But in Stylus, one would have to write the program something like this:
64+
65+
.. code-block:: solidity
66+
67+
contract C {
68+
bool inited;
69+
uint256 x;
70+
71+
function init(uint256 _x) public {
72+
require(!inited);
73+
x = _x;
74+
inited = true;
75+
}
76+
77+
function get_the_number() public view returns (uint256) {
78+
return x;
79+
}
80+
}
81+
4082
``block.number``
4183
________________
4284

0 commit comments

Comments
 (0)