solidity program compiler issue - solidity

pragma solidity ^0.8.17;
contract Greeter {
string greeting;
function Greeter(string _greeting) public{
greeting=_greeting;
}
function greet() constant returns (string){
return greeting;
}
}
ERROR
ParserError: Expected '{' but got 'constant'
--> project:/contracts/greeter.sol:7:22:
|
7 | function greet() constant returns (string){
i am compiling solidity program but there is issue when compiling

In the new compiler versions (0.4.21 above) the constructor and constant keyword deleted. Now, you must use for:
constructor: the following statement for:
constructor([parameters]) { // your logic }
constant: it depends on function accessibility that you give, in this case will be external. It allows to print the string that you initialized.
You must change your original smart in this way:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Greeter {
string greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() external view returns(string memory){
return greeting;
}
}

Related

Not able to inherit the One struct from another

When I tried to inherit one struct from another struct, I am getting this error:
from solidity:
ParserError: Expected '{' but got 'is'
--> contracts/Structure.sol:11:26:
|
11 | struct DerivedStruct is BaseStruct {
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Structure {
struct BaseStruct {
uint256 a;
uint256 b;
}
struct DerivedStruct is BaseStruct {
uint256 c;
uint256 d;
}
}
Got this error:
Can't we inherit one struct to another in Solidity?
You can inherit one sturct from another in Solidity.
Syntax you are using is incorrect. In solidity the 'is' keyword is used to inherit interfaces. To inherit form a struct you simply use its as the type of a new struct.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Structure {
struct BaseStruct {
uint256 a;
uint256 b;
}
struct DerivedStruct {
BaseStruct base;
uint256 c;
uint256 d;
}
}
DerivedStruct struct contains a BaseStruct field, which inherits its properties

What is the usage of override in Solidity?

I still don't understand the usage of override in solidity.
I know the how to write code of override, but I have no idea when I should use them.
Question 1: Is it just for write less code in inherited contract?
Question 2: If I override the function in son-contract, would it affect the function of father-contract?
Is it just for write less code in inherited contract?
Generally, you can use overriding if you want to change a default behavior. Here goes a silly example that assumes most vehicles have a steering wheel, so that's the default option - but it's overridden for motorbikes.
pragma solidity ^0.8;
contract Vehicle {
function turnRight() virtual external {
turnSteeringWheel();
}
}
contract Car is Vehicle {
// no need to override turning, car has a steering wheel
}
contract Motorbike is Vehicle {
// motorbike has a handlebar - not a steering wheel
// so turning a steering wheel would not work
function turnRight() override external {
turnHandlebar();
}
}
Overriding is also useful when you let someone else expand on your code. For example the OpenZeppelin ERC20 implementation allows you to override their default number of decimals.
pragma solidity ^0.8;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyContract is ERC20 {
constructor() ERC20("MyToken", "MyT") {}
function decimals() override public pure returns (uint8) {
return 2;
}
}
If I override the function in son-contract, would it affect the function of father-contract?
The function in the parent contract is not executed, unless the parent function is explicitly called with the super keyword.
pragma solidity ^0.8;
contract Parent {
function foo() virtual public pure returns (uint) {
return 1;
}
}
contract Child is Parent {
// not calling the Parent function, returning 2
function foo() override public pure returns (uint) {
return 2;
}
}
contract Child2 is Parent {
// calling the Parent function from within the Child function
function foo() override public pure returns (uint) {
uint returnedFromParent = super.foo();
// 1 plus 1, returns 2
return returnedFromParent + 1;
}
}

What's the difference between these 2 solidity interfaces methods?

I've been following a new journey into solidity for about a month now, so far so good, however with the Interface I'm having a hard time understanding the difference between these 2 methods of implementing Interfaces.
I can clearly notice how useful the Interface is in the 2nd method, but not in the 1st method.
Method 1:
pragma solidity ^0.8.0;
interface ICounter {
function count() external view returns (uint256);
function addToCount() external;
}
contract myInterface is ICounter {
uint256 counter = 0;
function count() external view override returns (uint256) {
return counter;
}
function addToCount() override external {
counter++ ;
}
}
Method 2:
pragma solidity ^0.8.0;
contract myContract {
uint256 count =0;
function increment() external {
count++ ;
}
}
pragma solidity ^0.8.0;
interface ICounter {
function count() external view returns (uint256);
function increment() external;
}
contract myInterface {
function incrementCount(address _counter) external {
ICounter(_counter).increment();
}
function getCount(address _counter) external view returns (uint256) {
return ICounter(_counter).count();
}
}
Interfaces in Solidity are mostly used for interacting with contracts deployed on other address, as you show in the Method 2.
Example of storing the pointer to another contract (assuming that it implements the interface) in a variable:
pragma solidity ^0.8.0;
interface ICounter {
function increment() external;
}
contract MyContract {
ICounter counter;
constructor(address _counter) {
counter = ICounter(_counter);
}
function incrementCount() external {
counter.increment();
}
}
In the Method 1, your code uses the is keyword for inheritance, which could be described as "myInterface is child of ICounter".
But there's no way to explicitly state implementation of an interface - that "myInterface implements ICounter". As you can see, you need to use the override modifier on the addToCount() function to state that you're overriding the parent function - which doesn't suggest implementing an interface at all.
I wouldn't consider the Method 2 a bad practice, as it can help you remember to implement all intended functions - otherwise the contract is treated as abstract and cannot be deployed. But for me personally, it's the less preferred method because there's no way to state an implementation instead of an inheritance (at least not in the current Solidity version 0.8).
Note: I'm aware that for example OpenZeppelin uses the Method 1 of "inheriting from an interface". I just don't agree with their approach.. :)

constructor through inheritance

so I'm trying out an example of using a constructor with inheritance but the remix ide is returning an error, which isn't the case on the course I'm following. Any idea of what might be causing the error or if there's an update with the newer solidity versions?
pragma solidity ^0.8.0;
contract myConstruct {
string public name;
constructor(string memory _name) {
name = _name;
}
}
contract mySecondConstruct is myConstruct {
constructor (string memory _name) myConstruct(_name) {} ;
}
The semicolon ; doesn't belong after the block. (Empty block in your case {})
Correct syntax:
// remove the semicolon
constructor (string memory _name) myConstruct(_name) {}

Kotlin use Java callback interface

I have a WebView. I want to call
public void evaluateJavascript(String script, ValueCallback<String> resultCallback)
this method.
Here is the ValueCallback interface:
public interface ValueCallback<T> {
/**
* Invoked when the value is available.
* #param value The value.
*/
public void onReceiveValue(T value);
};
Here is my kotlin code:
webView.evaluateJavascript("a", ValueCallback<String> {
// cant override function
})
Anyone have idea to override the onReceiveValue method in kotlin?
I tried the "Convert Java to Kotlin" but result is the next:
v.evaluateJavascript("e") { }
Thanks!
The following line is called a SAM conversion:
v.evaluateJavascript("e", { value ->
// Execute onReceiveValue's code
})
Whenever a Java interface has a single method, Kotlin allows you to pass in a lambda instead of an object that implements that interface.
Since the lambda is the last parameter of the evaluateJavascript function, you can move it outside of the brackets, which is what the Java to Kotlin conversion did:
v.evaluateJavascript("e") { value ->
// Execute onReceiveValue's code
}
You already are. The content between your braces is the content of the onReceive function. Kotlin has automatic handling for SAM conversions from Java. All of the following are equivalent.
// Use Kotlin's SAM conversion
webView.evaluateJavascript("a") {
println(it) // "it" is the implicit argument passed in to this function
}
// Use Kotlin's SAM conversion with explicit variable name
webView.evaluateJavascript("a") { value ->
println(value)
}
// Specify SAM conversion explicitly
webView.evalueateJavascript("a", ValueCallback<String>() {
println(it)
})
// Use an anonymous class
webView.evalueateJavascript("a", object : ValueCallback<String>() {
override fun onReceiveValue(value: String) {
println(value)
}
})