Skip to content

Function Overloading #781

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

Activity

  1. gdotdesign commented on Jul 5, 2025

    @gdotdesign
    Member

    Thank you for the detailed issue 🙏 Unfortunately, this is the intended behavior. Variants are not allowed as types (they can only be referenced as types in patterns).

    In your example, it's clear that the function only takes a variant as the parameter, but it's not feasible for bigger programs. In order to facilitate what you are asking for, we would need to implement function overloading. Most functional programming language do not implement function overloading because it usually has a runtime cost: basically we need to check at runtime which version of the function needs to run based on the parameters.

    What you wanted to do can be done using only the parent type (but it comes with a little boilerplate):

    fun processMoves(arr : Array(Modification)) : Array(Modification) {
      for item of arr {
        case item {
          Move => { item | ... } // Update the item
          => item              // Ignore other variants
        }
      }
    }
  2. changed the title [-]bug: passing a value of ADT variant to parameter array of ADT variant does not match[/-] [+]Function Overloading[/+] on Jul 5, 2025
  3. locked and limited conversation to collaborators on Jul 5, 2025
  4. converted this issue into a discussion #782 on Jul 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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