1
+ pragma solidity ^ 0.4.23 ;
2
+ contract Contract1 {
3
+
4
+ //Rule 1
5
+ // State Variables are storage variables
6
+ int public stateVar1 = 5 ;
7
+
8
+ // Rule 2 Starts
9
+ int public stateVar2 = 5 ;
10
+ int [3 ] public stateRefVar3 = [int (3 ),4 ,5 ];
11
+ // function parameter are memory data variables
12
+ function changeMemoryValueType (int a ) public returns (int ) {
13
+ a = stateVar2;
14
+ // changing a will not change stateVar2
15
+ a = 6 ;
16
+ return stateVar2;
17
+ }
18
+
19
+ // Rule 2
20
+ // function ref parameter are also memory data variables
21
+ function changeMemoryRefType (int [3 ] a ) public returns (int ) {
22
+ a = stateRefVar3;
23
+ a[1 ] = 45 ;
24
+ return stateRefVar3[1 ];
25
+ }
26
+
27
+ // Rule 3 Ends
28
+
29
+ // Rule 3 starts
30
+ int [2 ] public rule3StateRefVar = [int (1 ),2 ];
31
+ function demoRule3RefAndVal1 () public {
32
+ int a = 5 ; // Default to memoery
33
+
34
+ // Default to Storage but this will not work because it needs values
35
+ // from storage data type which can come from state variable
36
+ //int[2] arr = [int(1),2];
37
+
38
+ // b is default to storage in this case and need state variable
39
+ // changs in value of b will change rule3StateRefVar
40
+ // changs in value of rule3StateRefVar will change b
41
+ int [2 ] b = rule3StateRefVar;
42
+
43
+ // changs in value of d will NOT change rule3StateRefVar
44
+ // changs in value of rule3StateRefVar will NOT change d
45
+ int [2 ] memory d = rule3StateRefVar;
46
+
47
+ // overriding default form memory to storage
48
+ // if you want to use ref type then you have you use memory keyword
49
+ int [3 ] memory c = [int (11 ),12 ,13 ];
50
+
51
+ b[1 ] = 5 ; // rule3StateRefVar[1] also changed to 5
52
+ d[0 ] = 18 ; // rule3StateRefVar[0] will NOT change and stay 1
53
+ }
54
+
55
+ function demoRule3RefAndVal2 () public returns (string ) {
56
+
57
+ // Default to Storage but this will not work because it needs values
58
+ // from storage data type which can come from state variable
59
+ //string a = "Hello World";
60
+
61
+ // To make it work you need to provide memory keyword
62
+ string memory a = "Hello World " ;
63
+
64
+ // Value type can not be overridden to stroage they can only be memory
65
+ //int storage b = 45;
66
+
67
+ return a;
68
+
69
+ }
70
+
71
+ mapping (int => string ) keyValueList;
72
+ function demoRule3RefAndVal3 () public {
73
+
74
+ // local mapping variable must use state variable
75
+ // it can not be declared locally
76
+ mapping (int => string ) keyValueListLocal = keyValueList;
77
+ keyValueListLocal[23 ] = "Hello " ;
78
+ }
79
+
80
+ // Rule 3 ends
81
+
82
+ // Rule 4 does not have example
83
+
84
+ // Rule 5 starts
85
+ int public rule5StateVar1 = 10 ;
86
+ int public rule5StateVar2 = 20 ;
87
+ function demoStorageToStorageValueAssignment1 () public returns (int ) {
88
+
89
+ // Assignment of value type from storage variable to another storage
90
+ // variable creates copy so both maintain separate copy of data
91
+ rule5StateVar1 = rule5StateVar2;
92
+ rule5StateVar2 = 60 ;
93
+
94
+ return rule5StateVar1;
95
+ }
96
+
97
+ int [2 ] rule5StateVar3 = [int (1 ),2 ];
98
+ int [2 ] rule5StateVar4 = [int (3 ),4 ];
99
+ function demoStorageToStorageRefAssignment2 () public returns (int ) {
100
+
101
+
102
+ // This is diffrent from almost all programming languages
103
+ // as in other programming languages assignment of referance
104
+ // variable to another referance variable create referance and
105
+ // both variable points to same object
106
+ // BUT here in solidity it creates copy of ref variable and both
107
+ // points to different object
108
+ rule5StateVar3 = rule5StateVar4;
109
+ rule5StateVar4[1 ] = 10 ;
110
+
111
+ return rule5StateVar3[1 ]; // returns 4
112
+ }
113
+
114
+ // Rule 5 ends
115
+
116
+
117
+
118
+ // Rule 6 starts
119
+ int public rule6StateVar1 = 10 ;
120
+ function demoMemoryToStorageValueAssignment1 () public returns (int ) {
121
+ // Assignment from memory variable to storage variable creates copy
122
+ // so both maintain separate copy of data
123
+ int rule6LocalVar = 20 ;
124
+ rule6StateVar1 = rule6LocalVar;
125
+ rule6LocalVar = 60 ;
126
+
127
+ return rule6StateVar1; // return 20
128
+ }
129
+
130
+ int [2 ] rule6StateVar2 = [int (1 ),2 ];
131
+ function demoMemoryToStorageRefAssignment2 () public returns (int ) {
132
+ // Assignment from memory variable to storage variable creates copy
133
+ // so both maintain separate copy of data
134
+
135
+ // This is also diffrent from other programming languages
136
+ // In other language it creates referance and both variable points
137
+ // to same data
138
+ // BUT here both points to separate copy of data
139
+ int [2 ] memory rule6LocalVar1 = [int (3 ),4 ];
140
+
141
+ rule6StateVar2 = rule6LocalVar1;
142
+ rule6LocalVar1[1 ] = 10 ;
143
+
144
+ return rule6StateVar2[1 ]; // returns 4
145
+ }
146
+
147
+ // Rule 6 ends
148
+
149
+
150
+ // Rule 7 starts -- Nothing special same as rule 6 and 5
151
+ int public rule7StateVar1 = 10 ;
152
+ function demoStorageToMemoryValueAssignment1 () public returns (int ) {
153
+ // Assignment from storage variable to memory variable creates copy
154
+ // so both maintain separate copy of data
155
+ int rule7LocalVar = 20 ;
156
+ rule7LocalVar = rule7StateVar1;
157
+ rule7StateVar1 = 60 ;
158
+
159
+ return rule7LocalVar; // return 10
160
+ }
161
+
162
+ int [2 ] rule7StateVar2 = [int (1 ),2 ];
163
+ function demoStorageToMemoryRefAssignment2 () public returns (int ) {
164
+ // Assignment from storage variable to memory variable creates copy
165
+ // so both maintain separate copy of data
166
+
167
+ // This is also diffrent from other programming languages
168
+ // In other language it creates referance and both variable points
169
+ // to same data
170
+ // BUT here both points to separate copy of data
171
+ int [2 ] memory rule7LocalVar1 = [int (3 ),4 ];
172
+
173
+ rule7LocalVar1 = rule7StateVar2;
174
+ rule7StateVar2[1 ] = 10 ;
175
+
176
+ return rule7LocalVar1[1 ]; // returns 2
177
+ }
178
+
179
+ // Rule 7 ends
180
+
181
+
182
+
183
+ // Rule 8 starts -- This also same as others but the difference is
184
+ // assignment of memory to memory of referance type DO NOT create copy
185
+
186
+ function demoMemoryToMemoryValueAssignment1 () public returns (int ) {
187
+ // Assignment from memory variable to memory variable creates copy
188
+ // so both maintain separate copy of data
189
+ int rule8LocalVar1 = 20 ;
190
+ int rule8LocalVar2 = 30 ;
191
+ rule8LocalVar1 = rule8LocalVar2;
192
+ rule8LocalVar2 = 60 ;
193
+
194
+ return rule8LocalVar1; // return 30
195
+ }
196
+
197
+ function demoMemoryToMemoryRefAssignment2 () public returns (int ) {
198
+ // Assignment from memory variable to memory for referance variable
199
+ // DO NOT creates copy so both have same data
200
+
201
+ int [2 ] memory rule8LocalVar1 = [int (1 ),2 ];
202
+ int [2 ] memory rule8LocalVar2 = [int (3 ),4 ];
203
+
204
+ // This works are ref type, changes in one will change other
205
+ rule8LocalVar1 = rule8LocalVar2;
206
+ rule8LocalVar2[1 ] = 10 ;
207
+
208
+ return rule8LocalVar1[1 ]; // returns 10
209
+ }
210
+
211
+ // Rule 8 ends
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+ }
0 commit comments