Update a first-class module through include?

let animalUpdater = (a : module(Animal), weight: int) : module(Animal) => {
  let module(A) = a
  module({
    include A
    let weight = weight
  })
}

Result in:

function animalUpdater(a, weight) {
  return a;
}

but I was expected that the variable weight will override the value from the previous module.

Here is a link from playground: playground

Is it a bug ? A missing features ? Does a alternative way exist ?
For now I do the same by copying every type/variables (see animalNaiveUpdater from the playground), but I would like to know if I can just “spread” them or something similar

EDIT: Also, in the playground, why my module B is not a pure module ? Can’t see any side effect… is it because it come from a function so it’s potentially have different type during runtime (even if it doesn’t in my case) ? Why my module NaiveC is not mark as unpure ? Any way to make my module B pure ? Thank’s !

I manage to do what I wanted by passing through a MiniAnimal module type which contain the type/data I want to keep. See the playground. Hope it help some of us !

But I still wondering if it’s kind of a bug/missing feature or if it’s expected (and why)

The binding of function parameter weight is replaced when you do include A. It works if you do this:-

let animalUpdater = (a : module(Animal), weightArg: int) : module(Animal) => {
  let module(A) = a
  module({
    include A
    let weight = weightArg
  })
}
2 Likes

I want to add that, for this specific example, you would probably be much better off using records instead of first-class modules. The code will be easier to debug and require a fewer (or zero) type annotations. Using modules here instead of records is kind of like using a bulldozer where a simple shovel would suffice.

I realize that doesn’t answer the OP’s question, so feel free to disregard if you know that you need first-class modules anyway. :slightly_smiling_face: I just don’t want anyone to over-complicate a simple problem by trying to use FCM where they aren’t required.

1 Like

Yes I agree, it was just a simple example to illustrate my question. I should have specify it though.

EDIT: I made it with record instead for the reference, no type indeed and wayy easier. Playground