This is deprecated syntax that isn't required as of Solidity 0.4.0
function ()
is the 'default function' or 'fallback function'. If a transaction is sent to an address without transaction data, or if that data doesn't call a valid function on a contract, the default function is called instead.
throw
is a deprecated keyword to halt the computation and revert any changed state.
The function as written will force all transactions to fail if they don't call a legitimate function.
In the later versions of Solidity (>= v0.4.0), a transaction will throw if the default function is not explicitly implemented and the transaction has not called a legitimate function.
If a contract is to accept trivial transactions, such as being sent ether, the default function must be implemented as payable
. The amount of gas sent with a trivial send is not enough to change a state variable but is enough to log an event. So a typical implementation may be:
function () payable {
Deposit(msg.sender, msg.value);
}
As regards throw
, this keyword compiles to an illegal opcode which forces the EVM to halt the transaction and consumes all gas sent with the transaction. This has been replaced with the precompiled functions revert()
, assert()
and require()
to test for exceptions and halt the transaction on a failure.
function () payable {
// only owner can send
require(msg.sender == owner);
// Prevent overflow
assert(balance[owner] + msg.value >= balance[owner]);
// Accept only upto an amount
if(balance[owner] + msg.value > maximum) revert();
// Log the deposit
Deposit(msg.sender, msg.value);
// Careful, no guarantee there will be enough gas for this
balance[owner] += msg.value;
}