Skip to content

Latest commit

 

History

History
89 lines (46 loc) · 6.47 KB

README copy.md

File metadata and controls

89 lines (46 loc) · 6.47 KB

learning_solidity_by_example

Visibility and Getters

State Variable Visibility

public

Public state variables differ from internal ones only in that the compiler automatically generates getter functions for them, which allows other contracts to read their values. When used within the same contract, the external access (e.g. this.x) invokes the getter while internal access (e.g. x) gets the variable value directly from storage.

internal

Internal state variables can only be accessed from within the contract they are defined in and in derived contracts. They cannot be accessed externally. This is the default visibility level for state variables.

private

Private state variables are like internal ones but they are not visible in derived contracts.

Function Visibility

Solidity knows two kinds of function calls: external ones that do create an actual EVM message call and internal ones that do not. Furthermore, internal functions can be made inaccessible to derived contracts. This gives rise to four types of visibility for functions.

external

External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f cannot be called internally (i.e. f() does not work, but this.f() works).

public

Public functions are part of the contract interface and can be either called internally or via message calls.

internal

Internal functions can only be accessed from within the current contract or contracts deriving from it. They cannot be accessed externally

private

Private functions are like internal ones but they are not visible in derived contracts.

Getter Functions

The compiler automatically creates getter functions for all public state variables. For the contract given below, the compiler will generate a function called data that does not take any arguments and returns a uint, the value of the state variable data. State variables can be initialized when they are declared.

The getter functions have external visibility. If the symbol is accessed internally (i.e. without this.), it evaluates to a state variable. If it is accessed externally (i.e. with this.), it evaluates to a function.

If you have a public state variable of array type, then you can only retrieve single elements of the array via the generated getter function. This mechanism exists to avoid high gas costs when returning an entire array.


Function Visibility Specifiers

function myFunction() visibility_specifier returns (bool) { return true; }

public: visible externally and internally (creates a getter function for storage/state variables)

private: only visible in the current contract

external: only visible externally (only for functions) - i.e. can only be message-called (via this.func)

internal: only visible internally

Modifiers

pure for functions: Disallows modification or access of state.

view for functions: Disallows modification of state.

payable for functions: Allows them to receive Ether together with a call.

constant for state variables: Disallows assignment (except initialisation), does not occupy storage slot.

immutable for state variables: Allows exactly one assignment at construction time and is constant afterwards. Is stored in code.

anonymous for events: Does not store event signature as topic.

indexed for event parameters: Stores the parameter as topic.

virtual for functions and modifiers: Allows the function’s or modifier’s behaviour to be changed in derived contracts.

override: States that this function, modifier or public state variable changes the behaviour of a function or modifier in a base contract.