Ethereum: Type address is not default convertible in experienced structure
As an experienced Ethereum developer, you like familiarizing with the natives of the Ethereum ecosystem. In this article, we will deepen that agricultural intelligence to manipulate data in solidity, programming linguistics has a perspective with snow contrasts.
problem: Type address vs struct
In solidity, a structure is defined using a type of “address”, which represents a unique identifier is uncontrollable.
When you are “struct” in solidity, you complain a new data structure without specified fields and your appropriate type. Howver, if you try to attribute a value of a type of value, such as returning an structure an initializing season of the Sami type type, is not supervised in default conversion.
** Error: "Type address is not default convertible in ..."
It will be a meal suggests that it will be done in solidity not. The definition "Structure in your code is a specific compensation, and the type of non -use is not compatible.
Wy does that?
To understand why this problem arises, let's take a look at the way in which the structure and field allocations of the solidarity compiler. When you are "structured with multiplicity fields, solidity must have all fields that can be assigned in the correct order.
In your intense aid, because we take an admission fromStructure, the compiler is in court outside the type (eg{}
”, however, the “type of donations” returned by your function could not match an expected field type.
Code for example
Let’s consider a separate example for illustration this problem:
`Solidity
Pragma solidity ^0.8.0;
Structure Config {
Public network UPTT256;
}
GETNETWORKCONFIG () PUBLIC TOURNS (config) function {
Account Config ({
TOWORK: 1, // the type index is not default convertible in the structure
Version: 1,
Versionname: "1.0"
});
}
Re
In this functioning of the Getnetwork-Config returns a "configuration resort" to three fields (version andversionname
). Howver, Wen initialize another variable account, does not face the court outside the structural configure, but that of the compiler is its own value to key “Typres
Solution
To solve this problem, you can use more approaches:
- Use explicit conversions : Define the fields of your specific type (eg UNT256;
). This surrounds the fact that these are correctly used to assign the values.
Solidity
Pragma solidity ^0.8.0;
Structure Config {
Public network UPTT256;
}
GETNETWORKCONFIG () PUBLIC TOURNS (config) function {
Account Config ({
Network: 1,
Verse: 2, // is
Versionname: "2.0"
});
}
Re
- Use an alias type
: Define antype ‘alias address type’ in your code and use it:
`Solidity
Pragma solidity ^0.8.0;
Structure Config {
Public network UPTT256;
}
Type RetionConfig = address;
GETNETWORKCONFIG () Public Purre Return (NetworkConfig) function {
Return Config (1);
}
Re
2
` Solidity
Threshold solidity ^0.8.