top of page

Making Structs in Verse, Simplified

  • Writer: N/A
    N/A
  • Sep 22
  • 1 min read
ree

In Verse, structs are a clean way to group related data together. They’re great for representing objects with multiple properties, and they can even have operator overloads to make your code more expressive.


Here’s a FruitBasket that holds counts of fruit and a total weight:

FruitBasket<public> := struct<concrete>:
	Apples : int = 0
	Oranges : int = 0
	Bananas : int = 0
	BasketWeight : float = 0.0        

Operator Overloads

We can define operators to make combining baskets or scaling them up easier.


# Add two baskets together
operator'+'<public>(A : FruitBasket, B : FruitBasket)<transacts> : FruitBasket =
    {
        return FruitBasket	{
            Apples := A.Apples + B.Apples,
            Oranges := A.Oranges + B.Oranges,
            Bananas := A.Bananas + B.Bananas,
            BasketWeight := A.BasketWeight + B.BasketWeight
        					}
    }

Comments


bottom of page