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