Help with type inference


#1

Hi there. Quite new to OCaml and Bucklscript. I have what would seem to be a very small issue, but not sure exactly what is causing it.

If you look at the screenshot - its seems that adding a Js.Date.t field to my type somehow breaks type inference, or at least the editor (Vs Code) displaying the inferred type correctly.

Am I doing something really stupid here? Obviously these types are super simplified. In my full project, this one thing causes type inference to be completely broken across the whole project.


#2

I see a few things that look wrong:

  • you want , instead of ; in your record type definitions
  • you want : instead of = when youā€™re declaring records (also you donā€™t want to use ;)
  • you want to put a ; after each definition/declaration

e.g.

type t2 = {
  name: string,
  age: int,
  time_stamp: Js.Date.t,
};

Because you have an ā€œUNKNOWN SYNTAX ERRORā€ the compiler is not able to correct type check your code.


#3

@ethangodt seeing that he did use : instead of = in his type declaration. Did you mean you want : instead of = when declaring records?

@jpincas I think you want your b1 record to look like this:

let b1 = {
 name: "name",
 age: 10
};

and your b2 record should follow the same syntax.


#4

Sorry @jpincas, I misunderstood that your post is regarding ocaml and not reason. Please disregard my previous comment regarding records.

I think your t1 type declaration is missing a ; after age:int. It should look like this:

type t1 = {
 name: string;
 age: int;}


#5

(also didnā€™t realize this was OCaml specific question)


#6

Thanks - unfortunately thats not it. Adding the ; doesnā€™t make any difference.


#7

Are you sure your project has access to Bucklescript Module Js Library?


#8

I think so, as the code compiles and works. Is there something special I need to do to ā€˜importā€™ it?


#9

I recreated your example and I am getting no errors.

My guess is that you donā€™t have access to the Js library

Maybe the docs on installation would help?


#10

Thanks for your help Ben. Yes, those were there installation steps I followed. I definately have access to that library as the code works as expected. It seems to be an editor issue. In general I am really failing to get Merlin working, and Iā€™ve been trying all day! It works while there is no .merlin file but then as soon as I run bsb, the .merlin file gets generated and it just stops working. Iā€™ve probably messed something up in the config somewhere I guess. Thanks anyway.


#11

Yes, of course. Have you tried cleaning your build by running bsb -clean-world ? Maybe that will fix the issue with merlin.


#12

Yeah - im constanly trying that, deleting the lib directory, deleting and regenering the merlin fileā€¦


#13

Sorry I canā€™t be more help. I donā€™t use VS Code. Have you taken a look at doc regarding vs code config? Maybe this will help with the merlin problem.


#14

Also if this problem persists, you can probably get better guidance on #editorsupport channel on discord.


#15

If you didnā€™t solve this issue on the discord, could you share your .merlin?


#16

Sure. Hereā€™s the bsb generated .merlin:

FLG -ppx /Users/jon/src/github.com/jpincas/test-bsb/node_modules/bs-platform/lib/bsppx.exe
S /Users/jon/src/github.com/jpincas/test-bsb/node_modules/bs-platform/lib/ocaml
B /Users/jon/src/github.com/jpincas/test-bsb/node_modules/bs-platform/lib/ocaml

FLG -w -30-40+6+7+27+32..39+44+45+101
S src
B lib/bs/src
S src/bc
B lib/bs/src/bc
####BSB GENERATED: NO EDIT}

Just to clarify - I have two separate issues:

  1. Adding time_stamp: Js.Date.t to my record just kills (at least editor display of) type inference - although the project compiles fine.
  2. Once the .merlin file is generated, I no longer get any editor ā€˜helpā€™, either as I type or on save.

Unfortunately there are other issues too. I know I can just replace ā€œmerlinā€ with ā€œbsbā€ in the ā€œreason.diagnostics.toolsā€ config option, but that only works on save, and the ā€˜as-you-typeā€™ help is so nice when it works! Plus, for some reason, the ā€œbsbā€ option only compiles one of my files, and offers no messages as to why the others havenā€™t been compiled. Just to add that when running npm run build, the whole thing compiles without issue and all the .bs.js files appear, but on the next save, all but one of those files just dissapear!


#17

Record types with the fields of the same name cannot be inferred. You need to explicitly annotate this case.
Here in reason docs example how to annotate fields https://reasonml.github.io/docs/en/record.html#record-needs-an-explicit-definition