Home > Software engineering >  Type Mismatch (for a custom type)
Type Mismatch (for a custom type)

Time:01-16

I think the code is a better representation of the problem vs me explaining it with regular english.

This is the code for domain modelling:

module Domain.A exposing (..)

import Domain.B

type Size
  = Pound Float
  | Kilogram Float
  | Gram Float
  | Liter Float
  | Ounce Float
  | Count Int

type StockStatus 
  = InStock
  | OutOfStock

type alias CityWholesalerProduct =
  {
    cityWholesalerId : String,
    brand : String,
    productSku : Maybe String,
    sku : String,
    upc : Maybe String,
    description : String,
    status : StockStatus,
    price : Float,
    unit : Domain.B.Unit,
    pack : Int,
    size : Size
  }

and this is the code for the model inside Main.elm

-- MODEL

type alias Model =
  { cityWholesalerProducts : (List Domain.A.CityWholesalerProduct)
  , cityWholesalerProduct : Domain.A.CityWholesalerProduct
  }

init : Model
init = 
  Model 
    [] 
    {
      cityWholesalerId = "",
      brand = "",
      productSku = "",
      sku = "",
      upc = "",
      description = "",
      status = Domain.A.OutOfStock,
      price = 0.00,
      unit = Domain.B.Count,
      pack = 0,
      size = Domain.A.Count 0
    }

The problem is that I'm getting an error that says:

TYPE MISMATCH - The 2nd argument to `Model` is not what I expect:

34|   Model 
35|     [] 
36|#>#    {
37|#>#      cityWholesalerId = "",
38|#>#      brand = "",
39|#>#      productSku = "",
40|#>#      sku = "",
41|#>#      upc = "",
42|#>#      description = "",
43|#>#      status = Domain.A.OutOfStock,
44|#>#      price = 0.00,
45|#>#      unit = Domain.B.Count,
46|#>#      pack = 0,
47|#>#      size = Domain.A.Count 0
48|#>#    }

This argument is a record of type:

    { brand : String
    , cityWholesalerId : String
    , description : String
    , pack : Int
    , price : Float
    , productSku : #String#
    , size : Domain.A.Size
    , sku : String
    , status : Domain.A.StockStatus
    , unit : Domain.B.Unit
    , upc : #String#
    }

But `Model` needs the 2nd argument to be:

    #Domain.A.CityWholesalerProduct#

#Hint#: I always figure out the argument types from left to right. If an argument
is acceptable, I assume it is “correct” and move on. So the problem may actually
be in one of the previous arguments!

I'm new to Elm and FP in general and type mismatch errors are my biggest pain point and I've been stuck on this one for a while now.

CodePudding user response:

I'm surprised that the Elm compiler - which normally gives really helpful error messages - isn't more specific here. But the problem that I see is simply in those fields - productSku and upc - where your type alias says they should be of type Maybe String, but you have initialised with value "", which is simply a String.

It's not clear which part is wrong. If those are always supposed to be simple strings, then fix the type declaration, changing Maybe String to String in those places. But I assume the type declaration is likely as you intended, and the values are wrong. You can try this instead:

init = 
  Model 
    [] 
    {
      cityWholesalerId = "",
      brand = "",
      productSku = Just "",
      sku = "",
      upc = Just "",
      description = "",
      status = Domain.A.OutOfStock,
      price = 0.00,
      unit = Domain.B.Count,
      pack = 0,
      size = Domain.A.Count 0
    }

which should at least compile - but I can't comment on what the right values are for your intended business logic. It's not clear to me what Nothing would signify for these Maybe values, and whether that's really any different from Just "" - but it could well be that they are.

  • Related