@@ -312,49 +312,77 @@ const val = instance.getValue();
312
312
313
313
#### Overloaded functions
314
314
315
- You may find yourself in the situation of having a Truffle contract object that has multiple functions with the same name.
316
- You can invoke these "overloaded functions" just like you would a normal contract function. Truffle contract instances
317
- actually wrap web3's contract abstraction (` web3.eth.Contract ` ) and so when you call an overloaded function, it
315
+ Solidity supports a contract having multiple functions with the same name as long as the
316
+ function argument types differ. And so you may find yourself in the situation of having a
317
+ Truffle contract object with this property.
318
+ You can invoke these "overloaded" functions just like you would a normal contract function. Truffle contract instances
319
+ actually wrap web3's contract abstraction (` web3.eth.Contract ` ) and when you call an overloaded function, it
318
320
uses the same function resolution that web3 uses. However, we must give a warning that this overloaded function
319
- resolution is a bit dodgy and can resolve to the wrong function when you call it. So be careful!
321
+ resolution is often imprecise and can resolve to the wrong function when you call it.
320
322
321
- The more unambiguous way of calling these types of functions is to use the ` methods ` namespace on Truffle contract
322
- objects. Consider the following contrived code sample:
323
+ Consider the following contrived code sample:
323
324
324
325
``` solidity
325
326
contract MyContract {
326
327
uint256 public myUint;
327
328
string public myString;
329
+ string public myOtherString;
328
330
329
- function setValue(uint val) {
331
+ function setValues(uint256 val, string str ) {
330
332
myUint = val;
333
+ myString = str
331
334
}
332
335
333
- function setValue(string str) {
336
+ function setValues(string str, string otherStr) {
337
+ myString = str;
338
+ myOtherString = otherStr;
339
+ }
340
+
341
+ function setValues(uint256 val, string str, string otherStr) {
342
+ myUint = val;
334
343
myString = str;
344
+ myOtherString = otherStr;
335
345
}
336
346
}
337
347
```
338
348
339
- This Solidity contract contains two functions named ` setValue ` , each taking a different type of input. In your JavaScript you
349
+ This Solidity contract contains three functions named ` setValues ` , each taking a different type of input. In your JavaScript you
340
350
might do something like the following:
341
351
342
352
``` javascript
343
- await instance .setValue (" this is not string cheese" );
353
+ await instance .setValue (
354
+ 666 ,
355
+ " this is not string cheese" ,
356
+ " this is string cheese"
357
+ );
358
+ ```
359
+
360
+ As mentioned above, Truffle contract wraps web3 ` Contract ` objects and uses web3's overloaded function resolution.
361
+ To resolve these functions, web3's strategy is to look at the number of arguments that the function was invoked with and try
362
+ one that takes that many arguments. This works for some cases but is not ideal in that it
363
+ can invoke the incorrect function. Take, for example, the case where we do something like the following:
364
+
365
+ ``` javascript
366
+ await instance .setValue (" this is not string cheese" , " this is string cheese" );
344
367
```
345
368
346
- Truffle contract (internally using web3's overloaded function resolution) will see that you input a string and
347
- call the second method in the contract to set ` myString ` . Again however, sometimes when your contract is more
348
- complex there might be some ambiguousness and it might fail to resolve to the correct function. In cases where
349
- you want to explicitly specify which function you would like, you should do something similar to the following:
369
+ We know that in our contract there are two functions named ` setValues ` that take
370
+ two arguments. In the above case, we cannot be sure whether web3's resolution will resolve to the first or second
371
+ function. The more precise way to reference functions like this is to use
372
+ the ` methods ` property on the contract instance. The following is an example of how you
373
+ access these methods:
350
374
351
375
``` javascript
352
- await instance .methods [" setValue(string)" ](" this is not string cheese" );
376
+ await instance .methods [" setValue(string,string)" ](
377
+ " this is not string cheese" ,
378
+ " this is string cheese"
379
+ );
353
380
```
354
381
355
382
The ` methods ` property is an object whose keys are strings that correspond to the contract function's names
356
- and signatures. The values are the functions themselves. We recommend using this method of calling overloaded
357
- functions since it will unambiguously resolve to the correct function.
383
+ and signatures with no spaces. The values are the functions themselves. You can inspect the
384
+ ` methods ` property to see the key names in this object and match the function signature.
385
+ We recommend using this method of calling overloaded functions since it will unambiguously resolve to the correct function.
358
386
359
387
#### Processing transaction results
360
388
0 commit comments