Skip to content

Type is not compile-checked across 2 lambdas #748

@Namek

Description

@Namek

In my project I have a code snippet as following:

fun findFieldById (fieldId : String) : Maybe(RowField) {
  Array.findByAndMap(form.rows,
    (row : FormRow) {
      let field =
        Array.find(row.fields,
          (field : RowField) { Form.rowFieldId(field) == fieldId })

      {field != Maybe.Nothing, field}
    })
}

The return type is supposed to be Maybe(RowField) as the function signature defines it. However the compiler never told me that the final result of is rather Maybe(Maybe(RowField)) because the inner field variable is Maybe(RowField) instead of RowField. Then it all compiles, while all the switch cases on the result of this function do not ever match.

Let's rewrite it to a self-contained example:

component Main {
  fun render : Html {
    let rows = [
      { fields: [{id: "1"}, {id: "2"}] },
      { fields: [{id: "3"}, {id: "4"}] }
    ]

    let foundField = dbg findFieldById(rows, "3")

    <div>
      "If below we see no text then this doesn't work correctly:"
      
      case foundField {
        Nothing => "no found field"
        Just(field) => field.id
      }
    </div>
  }

  
  fun findFieldById (rows : Array(Row), fieldId : String) : Maybe(Field) {
    Array.findByAndMap(rows,
      (row : Row) {
        let field =
          Array.find(row.fields,
            (field : Field) { field.id == fieldId })
  
        {field != Maybe.Nothing, field}
      })
  }
}

type Row {
  fields : Array(Field)
}

type Field {
  id : String
}

Sandbox: https://mint-lang.com/sandbox/43lm0CU7xeTMZA

In this example:

  1. the ccase foundField doesnt render any text (it doesn't match in the runtime)
  2. the dbg returns Maybe.Just(Maybe.Just(Field { id: "3" })) to the console.
Image

Expectation: compiler should error about mismatching types in this case.

Comment: Of course my guess about inner lambda is just a guess, I do not know the exact reason the compiler doesn't catch that error.

Metadata

Metadata

Assignees

Labels

stdlibStandard library related

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions