I understand why "view" is useful when you create functions in solidity but I don't understand why would someone use pure?
I get that it does not ready or overwrite state variables but why is that useful when coding?
for example:
function add (uint _x , uint _y) public pure returns (uint) {
returns _x + _y;
Related
I'm implementing several variations on a base contract and I want the child to be able to inherit one or both of the variations. When I inherit from both, I get "TypeError: Derived contract must override function "foo". Two or more base classes define function with same name and parameter types."
Code is below, straight from Remix, with the two solutions I've come up with. Solution 1 requires re-implementing something like 10 functions in the child contract, each of which just call "super", which is pretty ugly. Solution 2 requires me to provide a contract for each combination of variations, and with 7 variations, that's ugly too. Any better solutions?
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "hardhat/console.sol";
abstract contract A {
function foo () public virtual {
console.log("A");
}
}
abstract contract B is A {
}
abstract contract C is A {
function foo () public virtual override {
console.log("C");
super.foo();
}
}
contract X is B {} // foo() prints A
contract Y is C {} // foo() prints CA
// Solution 1:
contract Z1 is B, C { // why do I have to override foo() again?
function foo () public override (A, C) {
super.foo();
}
}
// Solution 2:
contract Cprime is B { // duplicate C as child of B
function foo () public virtual override {
console.log("C");
super.foo();
}
}
contract Z2 is Cprime {} // this is what I'd like Z to look like
I need to make a function that will be called each time someone called my contract. And I need to do it without using modifiers because there are many functions in my contract and I use third-party contracts.
How can I do it?
You can inherit third-party contracts and make a new child contract and then modify each function with a modifier you defined in the child contract, that's the only way to do.
Example:
Contract ThirdParty {
address owner;
function f() public {}
}
Contract Child is ThirdParty {
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function f() public onlyOwner {}
}
I have a pretty "simple" problem.
class Main {
public static function main()
new Main();
public function new() {
var a = callbackFunc;
var b = callbackFunc;
if (a == b)
trace("success");
else
trace("Failed");
}
private function callbackFunc():Void {}
}
When compiled to the JavaScript target everything is fine... in Neko it traces "Failed"... Didn't find anything useful in the net that might explain the Problem... Any ideas?
Use Reflect.compareMethods() - this should work on all targets:
if (Reflect.compareMethods(a, b))
trace("success");
else
trace("Failed");
The comparison operator is not specified to always work on functions, it depends on the target.
I'd like a constant instance variable in my class. The following throws an error in the constructior (of course, since I'm assigning to a constant):
public class Rotation {
public const angle:Number;
public function Rotation( angle:Number ) {
this.angle = angle;
}
}
I assume there is some solution, since it is possible to create non-static constant members.
To provide a public member that cannot be set from outside, you can declare it as a get function
public class Rotation
{
private var _angle:Number;
public function Rotation(angle:Number = 0)
{
_angle = angle;
}
public function get angle():Number
{
return _angle;
}
}
We know all that constant is constant, but your code can work only
in standard mode because strict mode only allows a constant’s value to be assigned at initialization time.
Take a look on this little example from Adobe.
Hope that can help.
What are the main differences (advantages/disadvantages) of declaring functions inside the default constructor function of a new class and functions declared outside the default constructor function? Is the access to access modifiers the only reason to declare functions outside default constructor functions?
Thank you.
If you mean this:
public class MyClass {
public function MyClass() {
function myFunction() : void {
}
}
}
then the main difference is not only visibility, but scope: myFunction() is declared as a temporary function, which can only be called from within the same method. After execution of the constructor is done, the function is discarded and garbage collected, as would any temp variable. You can easily verify this: Simply add a "regular" member function and try to call myFunction() from there - compilation will fail. So, of course, will trying to access the function from another class.
If you were referring to declaring the function as a variable, and initializing it from within the constructor, the main difference is type safety.
Consider this standard declaration:
public class MyClass {
public function myFunction ( param1:String ) : String {
// doSomething
return myString;
}
public function MyClass() {
}
}
We've declared a member function with a strictly typed parameter and a strictly typed return type. If we try to access this function in a way that does not comply with the declaration, the compiler will throw an error, and compilation will fail, just as one would expect.
Now the same declaration from within the constructor:
public class MyClass {
public var myFunction : Function;
public function MyClass() {
myFunction = function ( param1:String ) : String {
// doSomething
return myString;
}
}
}
We've declared a strictly typed member variable of type Function that is initialized to do the same thing as the member function above. But its strictly typed parameter and return type are evaluated by the compiler only within the scope of the declaration - if, for example, you try to access the function specifying too many parameters:
myClassInstance.myFunction ("whatever", "and", "some", "more");
the compiler won't complain, because both the parameter and return type are now only checked at runtime, instead of at compile time (there would still be an error, of course). So the major disadvantage of this approach is the lack of compile time type checking - errors will occur at runtime and will thus be harder to debug.
One advantage of this approach is that we could exchange this function at any time:
myClassInstance.myFunction = function ( i:int ) : void { trace (i); };
This is perfectly legal and will obviously change the object's behavior significantly. If we would do the same in the member function example, the compiler would also throw an error.