Solidity Contract Unused Function/Parameter - solidity

function _postValidatePurchase (
address _beneficiary,
uint256 _weiAmount
)
pure internal
{
// optional override
}
When I compile I get this error in parts of my smart contract that mimic the function above: "Warning: Unused function parameter. Remove or comment out the variable name to silence this warning.uint256 _weiAmount". However, when I comment out the uint and weiAmount, I get these errors :
DocstringParsingError: Documented parameter "_beneficiary" not found in the parameter list of the function.
DocstringParsingError: Documented parameter "_weiAmount" not found in the parameter list of the function.
libs/openzeppelin/crowdsale/Crowdsale.sol:105:5: TypeError: Wrong argument count for function call: 2 arguments given but expected 0.
_postValidatePurchase(_beneficiary, weiAmount);
regarding this line:
_postValidatePurchase(_beneficiary, weiAmount);
}

"Warning: Unused function parameter" is not an error, just a warning. If you have a function with parameters you are not using inside the function, why do you need them? You can just remove them from the input. The only case I can think of is when you are overriding the function. In any case, a warning does not prevent the contract from being compiled. You can just ignore the warning.

Related

Could someone, please, explain me the implementation of the following "Kotlin Literal high order function"?

I am a newbie in Kotlin, I just started to learn it,
I get the following code example about literal/high order function:
fun myHigherOrderFun(functionArg: (Int)->String) = functionArg(5)
println ( myHigherOrderFun { "The Number is $it" })
prints "The Number is 5"
Which I have difficulty to understand: the function myHigherOrderFun get a lambda function as parameter but i can't understand, where is the (Int) input parameter? I see is passed in functionArg(5)... but i can't realize how is possible that?
Thanks in advance.
To start from the beginning, in Kotlin functions are first-class types, just like numbers and Strings and stuff.  So a function can take another function as a parameter, and/or return a function as its result.  A function which does this is called a ‘higher-order function’.
And that's what you have in your example!  The line:
fun myHigherOrderFun(functionArg: (Int)->String) = functionArg(5)
defines myHigherOrderFun() as a function which takes one parameter, which is itself a function taking a single Int parameter and returning a String.  (myHigherOrderFun() doesn't specify an explicit return type, so it's inferred to be a String too.)
The next line is probably where things are less clear:
println(myHigherOrderFun{ "The Number is $it" })
The first non-obvious thing is that it's calling myHigherOrderFun() with a parameter.  Because that parameter is a lambda, Kotlin lets you omit the usual (…), and use only the braces.
The other non-obvious thing is the lambda itself: { "The Number is $it" }. This is a literal function taking one parameter (of unspecified type).
Normally, you'd have to specify any parameters explicitly, e.g.: { a: Char, b: Int -> /* … */ }.  But if there's exactly one parameter, and you aren't specifying its type, then you can skip that and just refer to the parameter as it.  That's what's happening here.
(If the lambda didn't reference it, then it would be a function taking no parameters at all.)
And because the lambda is being passed to something expecting a function taking an Int parameter, Kotlin knows that it must be an Int, which is why we can get away without specifying that.
So, Kotlin passes that lambda to the myHigherOrderFun(), which executes the lambda, passing 5 as it.  That interpolates it into a string, which it returns as the argument to println().
Many lambdas take a single parameter, so it gets used quite a lot in Kotlin; it's more concise (and usually more readable) than the alternative.  See the docs for more info.

Can I enable the "Function doesn't return a value on all code paths" warning for "Value Type" functions?

I noticed some of my functions aren't throwing the "Function doesn't return a value on all code paths" warning, even though not all of their paths return a value. After some experimenting and research, it looks like functions that return a "Value Type" data type (as defined here) do not throw the warning, while functions that return a "Reference Type" data type will throw the warning. I would assume this is because Value Types have a default value (generally 0), while the default value for Reference types is Nothing.
So I can understand why the warning wouldn't generally be thrown for Value Type functions. However, sometimes it's important to make sure the function explicitly returns a value on all paths, even if that function returns a Value Type. Is it possible to enable this warning for functions that return a Value Type, like Boolean, double, or an enumerator functions?
EDIT: By request, here's an example of a function that does NOT throw the warning:
Function TestFunction() As Double
If Now.DayOfWeek = DayOfWeek.Monday Then
Return 10
End If
End Function
(if I change the return type to String, I DO get the warning)
Under the Project Properties > Compile, there's a section called "Warning configurations". In there, you can set if you want these type of condition as warning, error or none.
In this case, the condition "Function returning intrinsic value type without return value" may be set to "None". It would also be possible to disable it with #Disable Warning BC42353

Calling function from already deployed contract in solidity?

I want to know how to call a function from already deployed contract in solidity. I tried below one but it's throwing error and require without imorting the deployed contract
contract B {
watch_addr = 0x1245689;
function register(string _text) {
watch_addr.call(bytes4(keccak256("register(string)")), _text);
}
}
Can any one please tell me the solution?
error:browser/delegate.sol:14:31: TypeError: Invalid type for argument in function call. Invalid implicit conversion from bytes4 to bytes memory requested. This function requires a single bytes argument. If all your arguments are value types, you can use abi.encode(...) to properly generate it.
watch_addr.call(bytes4(keccak256(abi.encode("register(string)"))));
In version 5.0 Solidity had some breaking changes:
The functions .call() ... now accept only a single bytes argument. Moreover, the argument is not padded. This was changed to make more explicit and clear how the arguments are concatenated. Change ... every .call(signature, a, b, c) to use .call(abi.encodeWithSignature(signature, a, b, c)) (the last one only works for value types). ... Even though it is not a breaking change, it is suggested that developers change x.call(bytes4(keccak256("f(uint256)"), a, b) to x.call(abi.encodeWithSignature("f(uint256)", a, b)).
So, the suggested way to call other contract is like this:
pragma solidity ^0.5.3;
contract test3 {
address watch_addr = address(0x1245689);
function register(string memory _text) public {
watch_addr.call(abi.encodeWithSignature("register(string)", _text));
}
}
Also note added memory keyword: you now need to specify data location for function parameters of complex types:
Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables.

Initializing a field member in Kotlin that is a function

New to Kotlin, I have seen this code:
val myModule : Module = module {
viewModel { MyViewModel(get()) }
single { MyRepository() }
}
Looking at the Kotlin docs, it isn't clear to me what the braces mean after "module". Is module a function and the braces are used to initialize the function? If this is true, can you point me to the part in the Kotlin documentation that indicates this? I can't find anything in the docs that shows an example of this. Here is the link:
https://kotlinlang.org/docs/reference/properties.html
Note that your example seems like Koin code.
In a more general sense:
In kotlin when the last parameter of a function is another function ( see Higher order functions) you can put it outside the parenthesis, and if it is the only (non optional) parameter you can omit the parenthesis enterily.
In your example module viewModel and single are functions that take another function as their only parameter, this way you can pass the lambda defining this paramter directly without any parenthesis.
The braces mean that the module function receives a lambda as a parameter. http://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter

Explanation on Function literal with receiver in Kotlin

I was following this link https://kotlin.link/articles/DSL-builder-in-Kotlin.html to understand the builder implementation in Kotlin. I didn't understand the methods inside Builder class. Method name() receives Extension Function as an argument which receives nothing and returns String. And the caller calls name { "ABC" }. If the caller is passing String to name method, how does it translate to an Extension method which returns String ?
I tried following Kotlin documentation for Function literals with receivers but all had samples which returns Unit or refers to DSL Builders. Tried googling it as well to understand but no luck in grasping the concept.
The call to name { "ABC" } is a combination of two Kotlin conventions.
There is a convention that if the last parameter to a function is a function you can omit the parenthesis. Also since there are no parameters to the lambda, "ABC" is what is returned by it.
So the caller is actually passing a lambda in the form name ({() -> "ABC"}), rather than a String.
Looking at the example in the link, it doesn't look like the receiver is necessary for name(), which is why it could be misleading.