55. General Introduction To Variables & Types
Variables : Variables are ussed to store information to be referenced and manipulated in a computer program
Three main types of variables: Booleans, Integers, Strings
- Boolean : bool - true/false
- Integer : uint - Singed and unsigned integers of varying sizes
- String : string - data values that are made up of ordered sequences of characters
57. Writing Solidity Variables
pragma solidity >= 0.7.0 < 0.9.0;
contract learnVariables {
uint chocolateBar = 10;
uint storeOwner = 300;
bool lieDetector = true;
string errorMessageText = "Error!";
}
했던 걸 반복하는 느낌인데.. 이거 했다고 awsome! 넌 이제 뭐든 할 수 있어! 이러니까 뭔가 ㅋㅋㅋㅋㅋ 점점 빨라지는 재생속도..
60. Introduction To Decision Making - If Statements in Solidity
If statement: The if statement is the fundamental control statement that allows Solidity to make decisions and execute statements conditionally.
contract DecisionMaking {
uint orange = 5;
function validateOranges() public view returns (bool) {
if (orange == 5) {
return true;
} else {
return false;
}
}
}
갑자기 public 뒤에 view가 붙어서 당황했는데 나중에 설명해준다고... 하지만 찾아봤지
https://www.tutorialspoint.com/solidity/solidity_view_functions.htm
View functions ensure that they will not modify the state. A function can be declared as view. The following statements if present in the function are considered modifying the state and compiler will throw warning in such cases.
상태를 변경하지 않을 경우에 사용한다고 한다. 실제로 저 안에서 orange에 다른 값을 줘봤더니 warning이 뜨더라는
63. Arrays in Solidity - Pop Push & Length Methods
Array is a data structure, which stores a fixed-size sequential collection of elements of the same type.
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
contract learnArrays {
uint[] public myArr;
uint[] public myArr2;
uint[200] public myFixedSizeArr;
// push() method adds one or more elements to the end of an array and returns the new length of the array
function push(uint number) public {
myArr.push(number);
}
// pop() method removes the last elemenet from an array and returns that value
function pop() public {
myArr.pop();
}
// length is a string property that is used to determine the length of a string
function getLength() public view returns (uint) {
return myArr.length;
}
}
64. Arrays in Solidity - Delete
// when you delete in your array the length remains the same
function remove(uint index) public {
delete myArr[index];
}
myArr = [1, 2, 3] 이라고 할 때, remove(2) 를 호출하면 myArr = [1, 2, 0] 이 된다. 즉 value만 0으로 초기화되고 길이는 그대로인 것.
65. Exercise - Maintain a Compact Array
Exercise: create a function that can fully remove on item from an array
- Create an Empty array called changeArray
- Create a function called removeElement which sets the index argument of the array to the last element in the array
- remove the last index from that function with the pop method
- Create a function called test which pushes 1 2 3 4 into changeArray
- remove the element 2 from the array when the contract is called
쓰읍.. delete로는 데이터가 삭제가 안되니까, trick을 써서 삭제하고자 하는 인덱스의 value를 맨 마지막으로 옮겨서 pop으로 삭제하는 걸 구현해보라는데.. 이게.. 맞나..? 순서가.. 중요하지.. 않아...? 왜...?
66. Solution - Maintain a Compact Array
uint[] public changeArray;
function removeElement(uint i) public {
changeArray[i] = changeArray[changeArray.length - 1];
changeArray.pop();
}
function test() public {
for (uint i=1; i<=4; i++){
changeArray.push(i);
}
}
그래도 해보라니까 일단 했다. 찾아보니까 delete 함수밖에 없고 진짜 직접 만들어서 구현해야 하는 것 같다.
https://ethereum.stackexchange.com/questions/1527/how-to-delete-an-element-at-a-certain-index-in-an-array
67. What Are Abstract Contracts in Solidity
Abstract Contracts
Abstract Contract is one which contains at least one function without any implementation.
Such a contract is used as a base contract.
Generally an abstract contract contains both implemented as well as abstract functions.
Derived contract will implement the abstract function and use the existing functions as and when required.
// base
abstract contract X {
function y() public view virtual returns(string memory);
}
// derive
contract Z is X {
function y() public override view returns(string memory) {
return "hello";
}
}
위의 코드를 deploy하면 alert가 뜨는데, contract를 Z contract로 바꾼 후 deploy하면 된다.
abstract contract에 대해 더 검색해봤는데, 이전 버전에서는 abstract라는 keyward를 따로 붙이지 않았던 듯 하다. 그래서 당황하다가 그냥 최근 docs를 보고 해결.. 아무튼 요즘엔 붙이는 게 맞는 것 같다. (아니 근데 안 붙이는 버전이 2020이던데 정말 빠르다..)
https://docs.soliditylang.org/en/v0.8.12/contracts.html?highlight=abstract#abstract-contracts