Skip to content

Function Overloading #781

@Namek

Description

@Namek

Code example causing compiler error

type Modification {
  Create
  Move(id : String, afterId : Maybe(String))
}

component Main {
  fun render : Html {
    let arr = processMoves([Modification.Move("1", Maybe.Nothing)])
  
    <div>"Hello World!"</div>
  }

  fun processMoves(arr : Array(Modification.Move)) : Array(Modification.Move) {
    arr
  }
}

Issue

Function processMoves() expects an array of explicitly the Modification.Move variant, however the compiler simplifies the parameter to accept the whole type (accepting all variants), instead of specific variant of the type.

I believe this should be allowed because my workaround would be to create a struct for passing parameters and returning the result, which then I would have to map back to the ADT variant anyway, so there's more of manual code.

Alternatively, the function processMoves() should be errored first on the parameter type, i.e. it should be explicitly explained that ADT variants are not allowed as types. This option is less preferrable but it would specify the limits of the system.

Error

░ ERROR (CALL_ARGUMENT_TYPE_MISMATCH) ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

The 1st argument to a function is causing a mismatch.

The function is expecting the 1st argument to be:

  Array(Modification.Move)

Instead it is:

  Array(Modification)

The call in question is here:

    ┌ Main.mint:8:15
    ├────────────────────────────────────────────────────────────────────
   4│ }
   5│
   6│ component Main {
   7│   fun render : Html {
   8│     let arr = processMoves([Modification.Move("1", Maybe.Nothing)])
    │               ⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃⌃
   9│
  10│     <div>"Hello World!"</div>
  11│   }
  12│

Sandbox

https://mint-lang.com/sandbox/hRoQ-3GqY5xNhg

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions