'solidity assert for several not nulls
I have the following function in solidity:
function registerStudentEvaluation(string memory _firstName, string memory _lastName, string memory _subject, uint _note) public returns(Note memory) {
bytes32 studentHash = keccak256(abi.encodePacked(_firstName, _lastName));
bytes32 subjectHash = keccak256(abi.encode(_subject));
address teacherAddress = msg.sender;
assert (students[studentHash] && subjects[subjectHash] && teachers[msg.sender]);
Note memory newNote = Note(studentHash, subjectHash, msg.sender, _note);
bytes32 noteHash = keccak256(abi.encodePacked(studentHash, subjectHash, msg.sender, _note));
notes[noteHash] = newNote;
emit student_evaluated(noteHash);
return newNote;
}
And I am getting these errors:
TypeError: Operator & not compatible with types struct Notes.Student storage ref and struct Notes.Subject storage ref
--> notes.sol:71:17:
|
71 | assert (students[studentHash] & subjects[subjectHash] & teachers[msg.sender])
TypeError: Operator && not compatible with types struct Notes.Student storage ref and struct Notes.Teacher storage ref
TypeError: Invalid type for argument in function call. Invalid implicit conversion from struct Notes.Student storage ref to bool requested.
How can I assert for several not nulls in Solidity?
Solution 1:[1]
Solidity uses two ampersands && as the logical AND operator.
assert(true && true);
One ampersand & is the bitwise AND operator.
uint8 three = 3; // 00000011
uint8 five = 5; // 00000101
uint8 result = three & five; // 00000001
Docs: https://docs.soliditylang.org/en/v0.8.12/cheatsheet.html
Solution: Use logical operators in the assert() condition.
assert (students[studentHash] && subjects[subjectHash] && teachers[msg.sender]);
Note: It's not defined in your question but I'm assuming that the students property is defined as mapping(bytes32 => bool) students;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
