morphir-elm icon indicating copy to clipboard operation
morphir-elm copied to clipboard

Constructor usages cause compile errors in the generated Scala in certain cases

Open AttilaMihaly opened this issue 2 years ago • 1 comments

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:

  1. Define the following model in Elm:
type Custom
    = Ctor1
    | Ctor2


use : Bool
use =
    target Ctor1 Ctor2


target : a -> a -> Bool
target a b =
    True
  1. Run 'morphir make' and morphir-elm gen
  2. Compile the generated Scala.
  3. 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.

AttilaMihaly avatar Sep 13 '22 11:09 AttilaMihaly

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.

AttilaMihaly avatar Sep 13 '22 11:09 AttilaMihaly