Dynamic types via Variants


Context


Demo: Summing Integers and String Representations

This demo illustrates how to sum elements in an array that may be either integers or strings representing integers. It uses Mojo’s Variant to accommodate mixed data types within the same array. Notably, the data type of variable v3, whether String or Int, is determined only at runtime in this demo (dynamic type).


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from random import seed,random_ui64
from utils.variant import Variant
 
alias IntOrString = Variant[Int, String]

fn sum(*elements:IntOrString) -> Int:

    var res:Int = 0 

    for i in range(len(elements)):
        if elements[i].isa[String]():
            try:
                res += int(elements[i].get[String]()[])
            except e:
                print("Error:", e,elements[i].get[String]()[])
        else:
            res += elements[i].get[Int]()[]
    return res 
    

fn main():

    seed()
   
    var v1 = IntOrString(4)
    var v2 = IntOrString(String("12"))
    
    var v3:IntOrString

    if random_ui64(0, 1):
        print("v3 = String(21)")
        v3 = String("21")
    else:
        print("v3 = 21")
        v3 = 21

    print("result:",sum(v1,v2,v3))

Output:

v3 = String(21)

result: 37