Solidity Value Types
Value types are types where variables store their data directly in memory. When used in function arguments or assignments, the value of these variables is always copied. This means any changes made to the copied variable do not affect the original variable.
Let’s look at a few important types
contract Example {
uint a;
int b;
bool c;
enum Choice { Up, Down, Left, Right }
Choice choice = Choice.Up;
}A contract is similar to a class in object-oriented programming languages like Java, C++, or Python
Unsigned Integer
A non-negative whole numbers
contract Example {
// uint can be declared in steps of 8
// where the number represents the number of bits
uint8 a; // 0 -> 255
uint16 b; // 0 -> 65535
uint32 c; // 0 -> 4294967295
uint64 d; // 0 -> 18446744073709551615
uint128 e; // 0 -> 340282366920938463463374607431768211455
uint256 f; // 0 -> 115792089237316195423570985008687907853269984665640564039457584007913129639935
// uint is an alias for uint256
uint z1;
uint256 z2;
}Signed Integer
A number that could be either positive or negative
contract Example {
// uint can be declared in steps of 8
// where the number represents the number of bits
int8 a; // -128 -> 127
int16 b; // -32768 -> 32767
int32 c; // -2147483648 -> 2147483647
int64 d; // -9223372036854775808 -> 9223372036854775807
int128 e; // -170141183460469231731687303715884105728 -> 170141183460469231731687303715884105727
int256 f; // -57896044618658097711785492504343953926634992332820282019728792003956564819968 -> 57896044618658097711785492504343953926634992332820282019728792003956564819967
// int is an alias for int256
int z1;
int256 z2;
}Boolean
Either true or false
import "forge-std/console.sol";
contract Example {
constructor(bool myCondition) {
if(myCondition) {
// will log yay if myCondition is true
console.log("yay!");
}
}
}Constructor is a function that runs once upon deployment
Enums
Defining options for a value by name
import "forge-std/console.sol";
contract Example {
enum Choice { Up, Down, Left, Right }
constructor(Choice choice) {
if(choice == Choice.Up) {
console.log("up");
}
if(choice == Choice.Down) {
console.log("down");
}
}
}Hands-on
learn-solidity-presentations\1a-value-types\examples\0-example-value-types\src\Example.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import "forge-std/console.sol";
contract Example {
uint8 a = 255; // 0 -> 255
uint256 b = 22; // alias: uint
int8 c = 127; // -128 -> 127
int256 d = -55; // alias: int
bool myCondition = true;
bool myCondition2; // default value is false
enum Choice {
Up,
Down,
Left,
Right
}
Choice choice = Choice.Up;
constructor() {
console.logUint(type(uint8).min);
console.logUint(type(uint8).max);
console.logInt(type(int8).min);
console.logInt(type(int8).max);
if (myCondition) {
console.log("yay");
}
if (choice == Choice.Up) {
console.log("up");
}
if (choice == Choice.Down) {
console.log("down");
}
if (choice == Choice.Right) {
console.log("right");
}
if (choice == Choice.Left) {
console.log("left");
}
}
}edo@DESKTOP-4POSOVK:/mnt/e/Programming/Solidity/learn-solidity-presentations/1a-value-types/examples/0-example-value-types$ forge test -vv
[⠰] Compiling...
No files changed, compilation skipped
Ran 1 test for test/Example.t.sol:ExampleTest
[PASS] testExample() (gas: 187)
Logs:
0
255
-128
127
yay
up
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 20.89ms (180.10µs CPU time)
Ran 1 test suite in 91.14ms (20.89ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)
Learn Solidity. Alchemy University.
Accessed October 7, 2024
https://university.alchemy.com/overview/solidity