Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs that govern the behavior of accounts within the Ethereum state.
Solidity is a curly-bracket language designed to target the Ethereum Virtual Machine (EVM). It is influenced by C++, Python, and JavaScript. You can find more details about which languages Solidity has been inspired by in the language influences section.
Solidity is statically typed, supports inheritance, libraries, and complex user-defined types, among other features.
// SPDX-License-Identifier: GPL-3.0pragma solidity^0.8.8;structIndexValue{uintkeyIndex;uintvalue;}structKeyFlag{uintkey;booldeleted;}structitmap{mapping(uint=>IndexValue)data;KeyFlag[]keys;uintsize;}typeIteratorisuint;libraryIterableMapping{functioninsert(itmapstorageself,uintkey,uintvalue)internalreturns(boolreplaced){uintkeyIndex=self.data[key].keyIndex;self.data[key].value=value;if(keyIndex>0)returntrue;else{keyIndex=self.keys.length;self.keys.push();self.data[key].keyIndex=keyIndex+1;self.keys[keyIndex].key=key;self.size++;returnfalse;}}functionremove(itmapstorageself,uintkey)internalreturns(boolsuccess){uintkeyIndex=self.data[key].keyIndex;if(keyIndex==0)returnfalse;deleteself.data[key];self.keys[keyIndex-1].deleted=true;self.size--;}functioncontains(itmapstorageself,uintkey)internalviewreturns(bool){returnself.data[key].keyIndex>0;}functioniterateStart(itmapstorageself)internalviewreturns(Iterator){returniteratorSkipDeleted(self,0);}functioniterateValid(itmapstorageself,Iteratoriterator)internalviewreturns(bool){returnIterator.unwrap(iterator)<self.keys.length;}functioniterateNext(itmapstorageself,Iteratoriterator)internalviewreturns(Iterator){returniteratorSkipDeleted(self,Iterator.unwrap(iterator)+1);}functioniterateGet(itmapstorageself,Iteratoriterator)internalviewreturns(uintkey,uintvalue){uintkeyIndex=Iterator.unwrap(iterator);key=self.keys[keyIndex].key;value=self.data[key].value;}functioniteratorSkipDeleted(itmapstorageself,uintkeyIndex)privateviewreturns(Iterator){while(keyIndex<self.keys.length&&self.keys[keyIndex].deleted)keyIndex++;returnIterator.wrap(keyIndex);}}// How to use itcontractUser{// Just a struct holding our data.itmapdata;// Apply library functions to the data type.usingIterableMappingforitmap;// Insert somethingfunctioninsert(uintk,uintv)publicreturns(uintsize){// This calls IterableMapping.insert(data, k, v)data.insert(k,v);// We can still access members of the struct,// but we should take care not to mess with them.returndata.size;}// Computes the sum of all stored data.functionsum()publicviewreturns(uints){for(Iteratori=data.iterateStart();data.iterateValid(i);i=data.iterateNext(i)){(,uintvalue)=data.iterateGet(i);s+=value;}}}