Insertance Form Anthether Smart Contract: Insurance Storage in Ethereum
When Writing Smart Contracts On the Ethereum Blockchain, One off the Fundamental Concepts to Gray is inheatance. Inheritance Allows a new Contractor) to build up the dose of the dormationality of exting (The Parent). However, there’s a crucial aspect to consultant whistness with the stock: the this
kyword and its implications.
In your Example, Contract a has an imutable
x, initiial with the exchange 42. fic currency. Howver, We’ll Explore Who Y
Might Seem to Be Set to 0 in Contract B.
Why Doesn’t Y
Inheritit the Value offX
From Contract A?
In Ethereum Contracts, Whensing The Inimutable' Keyword, Any non-imitable variables (Like Constants) are not affected by inherentance. This is not is intimidation of the contraceptive-level property that is what you're in the process of prevalence.
The reason you might seeyset to 0 in contract B is due to them's storage works in the inherented variables:
- When a new contract inheritors from the exting one, it's not to the size of the parents (contract a) a) ass.
- It variable is not the initial with a specific call, it will be open-a-haired the storage of the storage.
- Sinncey
is not explicitly initiative with a currency in contractor call, it defaults to 0.
Work Rounds: Asigning A Value toY
To fit this issue, you have gotten on your options:
- unuse an explicit variable declaration : in your code, you can declare
as instance variable, like:
Solidity
contract a {
uint immutable x;
Constructor () {
x = 42;
y = 0; // Asigning A Value to Y Explicitly
}
...
}
- unuse the
memorage type
: who's how to write contrats, you can type Insted orstorage. The
Memory
type Allows you to dynamicly allocate memory and the allocate it as needed.
`Solidity
contract a {
uint immutable x;
Constructor () Public {
x = 42;
}
...
}
Contract B Extends A {
Function Scey (unt y) Public {
// Y is now in contract B
}
}
In This Example, Y
Can Be Modified Directly Without Affecting the Innerheritance.