G-d willing
Hello,
Is there a way to write a function that returns a different data type according to it’s input?
For example, I want to achieve something like this - I know it does not compile, It is just to emphasize what I would like to achieve.
data DataType1 = DataType1 with
someInt : Int
data DataType2 = DataType1 with
someText : Text
data DataType3 = DataType1 with
someDecimal : Decimal
funcTest : Int -> ????
funcTest value = case value of
| 1 -> return DataType1 with someInt = 5
| 2 -> return DataType2 with someText = "hello"
| Otherwise -> return DataTy[e3 with someDecimal = 20.3
Currently, the way I can solve it is by creating a new data type that will store all of the 3 data types like the following:
data AllDataTypes = AllDataTypes with
dataType1: Optional DataType1
dataType2: Optional DataType2
dataType3: Optional DataType3
And according to the inout argument I will fill the correct data type, something like this:
funcTest : Int -> AllDataTypes
funcTest value = case value of
| 1 -> return AllDataTypes with
dataType1 = Some DataType1 with someInt = 5
dataType2 = None
dataType3 = None
| 2 -> return AllDataTypes with
dataType2 = Some DataType2 with someText = Hello
dataType1 = None
dataType3 = None
|Otherwise return AllDataTypes with
dataType3 = Some DataType3 with someDecimal = 20.3
dataType1 = None
dataType2 = None
Thanks