Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record instance in F# is null in C# [closed]

I'm working on an assignment for school in which we make a small game using monogame, with the added challenge of working in F#. The game logic is fully immutable F#, and the entrypoint is in C# by use of the Game class in monogame. I have however come across a weird problem concerning Record types in F#. In the logic (F#) I have something along the lines of:

...
module Vectors =
    type Vector = {
        x : double
        y : double
    }

    let Zero : Vector = {x=0.0; y=0.0}
... 

And in C# I have some code accessing Zero:

...
player.vector = Vectors.Zero;
...

Weirdly enough, when I try to now use player.vector, it comes up as null. When debugging Vector.Zero is also null. I've looked around for a few solutions, and it may be some trivial mistake, but I can't seem to find it.

like image 514
Dfctps Avatar asked Nov 16 '17 10:11

Dfctps


2 Answers

When you build an F# project as an exe, but use it as a library, occasionally some initialisation code doesn't get called.

This can be fixed by changing the project to build as a library.

like image 81
John Palmer Avatar answered Sep 22 '22 05:09

John Palmer


Solved - F# project was building as a console application. Changing it to be a class library fixed the issue.

Thanks @JohnPalmer

like image 31
Dfctps Avatar answered Sep 23 '22 05:09

Dfctps