Search code examples
dhall

dhall Invalid alternative type


I've defined a union type with two elements like so:

FunctionCode.dhall

{- Function Code union -}

let StaticFC = ./StaticFunctionCode.dhall
let DynamicFC = ./DynamicFunctionCode.dhall

in < Static : StaticFC | Dynamic : DynamicFC >

The two types, StaticFC and DynamicFC are defined like so:

StaticFunctionCode.dhall

let FieldList = ./FieldList.dhall
let FunctionCodeType = ./FunctionCodeType.dhall

in
{ Type =
    { dlc : Natural
    , fields : FieldList
    , telemjson : Text
    , fct : Natural
    , timestamp : Optional Text
    }
, default = { timestamp = None Text, fct = FunctionCodeType.Static }
}

DynamicFunctionCode.dhall

let FieldTypeID = ./FieldTypeID.dhall
let OptionList = ./OptionList.dhall
let FunctionCodeType = ./FunctionCodeType.dhall

in
{ Type =
    { idtype : FieldTypeID
    , idindex : Natural
    , options : OptionList
    , fct : Natural
    , timestamp : Optional Text
    }
, default = { timestamp = None Text, fct = FunctionCodeType.Dynamic }
}

Ignoring the other types defined, here is how I'm trying to write a let statement using this union:

let fc1 =
  FunctionCode.Dynamic FunctionCode.Dynamic::{
     idtype = FieldTypeID.U8
   , idindex =  0
   , options = [...]
   }

in {fc1}

When I run dhall-to-json on this I get the following error:

Error: Invalid alternative type

6|  < Static : StaticFC | Dynamic : DynamicFC >

FunctionCode.dhall:6:4

I've tried some different combinations on how to use this defined union but no luck. What am I missing?


Solution

  • The problem is that StaticFC and DynamicFC are values, not types. But types are needed to define the FunctionCodeType union type.

    You can easily fix this by accessing the Type fields contained within StaticFC and DynamicFC:

    let StaticFC = ./StaticFunctionCode.dhall
    let DynamicFC = ./DynamicFunctionCode.dhall
    
    in < Static : StaticFC.Type | Dynamic : DynamicFC.Type >