Record instance in F# is null in C# [closed]
Asked Answered
L

2

7

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.

Lalittah answered 16/11, 2017 at 10:28 Comment(7)
Positions ? In your code the module has name VectorsMaraca
I'm sorry, slight mistake in the post. Vectors.Zero is null thereLalittah
Do you have a stacktrace when it fails? Does it fail on that specific line?Berdichev
@Berdichev it fails when trying to update player.vector, when updating it simply increases the vector's x and y for now, but it fails as it gets a null. StacktraceLalittah
My guess - you have built the F# code as a .exe rather than a dllQuay
@John Palmer oh.. let me check thatLalittah
@JohnPalmer Thanks, that fixed it. Weirdly the F# project was set up as a console application, must have overlooked that when setting up the solutionLalittah
Q
11

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.

Quay answered 16/11, 2017 at 11:31 Comment(0)
L
3

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

Thanks @JohnPalmer

Lalittah answered 16/11, 2017 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.