morphir-elm
morphir-elm copied to clipboard
Constructor usages cause compile errors in the generated Scala in certain cases
Describe the bug While constructors are themselves not types in Morphir (they are values of the custom type they belong to) the Scala mapping of a sealed trait hierarchy with case object/classes makes them types as well. This causes type inference issues where two constructors or a constructor and its parent type become incompatible.
To Reproduce Steps to reproduce the behavior:
- Define the following model in Elm:
type Custom
= Ctor1
| Ctor2
use : Bool
use =
target Ctor1 Ctor2
target : a -> a -> Bool
target a b =
True
- Run 'morphir make' and
morphir-elm gen
- Compile the generated Scala.
- See error:
type mismatch;
found : morphir.sdk.Foo.Ctor2.type
required: morphir.sdk.Foo.Custom.Ctor1.type
target(Ctor1)(Ctor2)
Expected behavior The generated code should compile.
The fix here is to add type ascriptions to every occurrence of type constructors to reproduce the semantics of Morphir in Scala. Here's how that looks like in the above example:
def use: morphir.sdk.Basics.Bool =
target(Ctor1: Custom)(Ctor2: Custom)
This ascription is safe to put in everywhere. In some cases it's unnecessary but it will make sure that the Scala compiler knows what we are trying to do in every situation.