Enumeration

It's fantastic. SpaceMuffin now has fans and fans have levels. However, what does a level number means? Associating number to human readable string is done easily with enumeration.

What is it

Enumeration (enum) is a way to associate number to string by the mean of type declaration. For example, a SpaceMuffin fan can be one of the following { Fan, Excited, InLove, CrazyInLove }.

Use

Declaration

You declare an enum as follow

// Still `CamelCase` and Capitalised
// Notice the `s` because there are multiple directions
contract MyContract {
  enum Directions { Top, Right, Bottom, Left }

  Directions public d;

  // ...
}

Amiral tip There is NO ; at the end of the declaration and NO = in the middle

Exercise
Correct!
False!

Loading...

Declare Levels enum type. It contains, in that order { Fan, Excited, InLove, CrazyInLove }. Also declare sampleLevel, a public Levels variable
pragma solidity ^0.4.24; contract SpaceMuffin { // start here :) struct Fan { address contact; uint level; // `level` should be of type `Levels` } Fan[] public fans; // and put `sampleLevel` down here ;) // Don't worry, we will update the conrtact fully }

Members

Members of an enumeration are accessed the same way as members of a structure.

enum Directions { Top, Right, Bottom, Left }

Directions public north = Direction.Top;

Exercise
Correct!
False!

Loading...

Complete updateFan function by increasing the level of i-th element of fans by 1
pragma solidity ^0.4.24; contract SpaceMuffin { enum Levels { Fan, Excited, InLove, CrazyInLove } struct Fan { address contact; Levels level; } Fan[] public fans; function like() public { // Level is not equal to `1` but to `Fan` fans.push(Fan({contact: msg.sender, level: 1})); } // don't care about out of bound indexes function updateFan(uint index) public { // we shall complete this function in the next exercise } }

Hint

  • When you access an enum, you should put its name before like for Direction.Top

Default

By default, an enum is initialised to its first element.

enum Directions { Top, Right, Bottom, Left }

Directions public north1; // not readable
Directions public north2 = Directions.Top; // readable

// north1 == north2

Enum and Integers

Conversion

At the beginning of this lesson, you were told enumerations where a way to associate string to integers. And that's true.

You can convert uint to enum type and vice-versa. It associated integer is the position of the value in the enumeration (starting with 0)

enum Directions { Top, Right, Bottom, Left }

Directions public enorth = Direction(0); // resolves to Direction.Top
uint public unorth = uint(Directions.Top); // resolves to 0

Exercise
Correct!
False!

Loading...

Rewrite updateFan and access functions
pragma solidity ^0.4.24; contract SpaceMuffin { enum Levels { Fan, Excited, InLove, CrazyInLove } struct Fan { address contact; Levels level; } Fan[] public fans; modifier withinBounds(uint index) { require(index < fans.length); _; } modifier notMaxLevel(Levels level) { // Yes! It's so crazy right now! require(level != Levels.CrazyInLove); _; } function like() public { fans.push(Fan({contact: msg.sender, level: Levels.Fan})); } function updateFan(uint index) public notMaxLevel(fans[index].level) { // let's update the level of fan-ness fans[index].level += 1; } // update this function to return a Levels variable function access(uint index) view public withinBounds(index) returns (address, uint) { return (fans[index].contact, fans[index].level); } }

Hint

  • Functions can return enums without any problem
  • You can have a look at notMaxLevel modifier
  • You have to use type conversion twice

results matching ""

    No results matching ""