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

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
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.

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.

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

Related

Why does unity say that i have ; missing when i checked everything [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I get NetworkButtons.cs(9.87): error cs1002: ; expected. And i cant find the reason of it so here's my c# code
Code
I tried finding if ; was missing somewhere but i cant find it
If( ... ) is not correct. The keyword should be if( ... ) (all lower-case).
Because of the typo, the compiler first treats If() as a function call to a (probably non-existent) If(bool) function. Following the function call there is a missing ;, just like the compiler says, and following that an anonymous code block that will always run, regardless of the result of the If() function.
Now no such function exists (unless you made one), but the compiler doesn't get that far. If you try to fix this by adding the missing ; instead of fixing the keyword you'll get a new error complaining about the missing function definition.

nUnit Integration test weird interaction with other test [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a bunch of tests which execute basically a file being moved around.
At the end I check if the file exists in the target directory.
I have 8 such tests which all look the same except for some minor differences.
One of the tests fails when run together with one specific other test (we call it: fTest). I found the culprit codeline and it is this simple assert running in a suceeding test (sTest). When I comment the Assert line, fTest does no longer fail.
var exists = File.Exists(#"\\?\" + destFullPath);
Assert.AreEqual(true, exists);
Even when I remove the first line and change the second line to Assert.AreEqual(true, true) the fTest still fails. Only commenting it out works.
This is some really weird behavior and I have absolutely no clue what is going on. How can one test affect the other?
EDIT: I just copied the sTest, deleted the original and renamed the copy to "TEST()" and now fTest works. What the f*?
I fixed it. Since these are integration tests there was one test which needed to run first. I added the order:1 attribute and now it works.

Why accessing a class field via a struct holding a reference to it is faster in C#? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
A few days ago I checked out the C# implementation of Binary Trees test # Computer Language Benchmark Game, and was weirdly surprised: the tree node there is described as a struct (TreeNode) referencing a class (Next) which has two fields of a TreeNode (strut) type. This obviously looks weird, so I updated this code to use a single class (i.e. ~ class TreeNode { TreeNode Left, Right }). My ItemCheck implementation was:
public int ItemCheck()
{
if (ReferenceEquals(Left, null)) // Such a node is always a leaf in this test
return 1;
return 1 + Left.ItemCheck() + Right.ItemCheck();
}
As you may find, it's quite similar to the original implementation. Nevertheless, the performance of this code was ~ 2.2x worse!
Can someone explain why this kind of "optimization" makes sense on .NET? I mostly want to understand what are some other implications of this - of course, if it's not simply a lack in C# / JIT compiler.
A bit more readable version of the same code with a few minor performance improvements can be found here: https://github.com/alexyakunin/BenchmarkGame-CSharp/blob/master/src/BinaryTrees/Program.cs
Update: I created an extra project for benchmarking this:
Code: https://github.com/alexyakunin/BenchmarkGame-CSharp/blob/master/src/Benchmarks/Program.cs
Benchmarking results: https://github.com/alexyakunin/BenchmarkGame-CSharp/blob/master/src/Benchmarks/BenchmarkDotNet.Artifacts/results/TreeNodeBenchmark-report-github.md

Is it possible to make a program that reads its own source code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What I mean is, could one possibly make a program that does the equivalent of
public class PrintOwnSourceCode
{
public static void Main ( )
{
System.Console.WriteLine([something]);
// prints "public class PrintOwnSourceCode { public static void Main ( ) { ... } }"
}
}
???
And would that be an example of reflection?
Somewhat.
Decompilers can do something similar to this:
I just decompiled a decompiler so I could use it to decompile itself
.NET Decompilers, like [.NET Reflector] (http://www.red-gate.com/products/dotnet-development/reflector/) and dotPeek are capable of reflecting upon a .NET assembly and generating files that resemble the source code. It will not look exactly like the source code because compiling and decompiling is kind of like translating English to French and then back to English--the results are not always guaranteed to be 1:1 as Google Translate can demonstrate. Information, like whitespace, that are for easy reading but not required by the compiler will be lost in the decompilation process. So, your application could decompile itself or invoke an external decompiler to print itself.
Aside
In compiled languages, the compiled code does not have direct access to the source code. (Companies don't typically ship the source code with the compiled code to customers. They only ship the compiled executable.) When it comes to parsed languages, like JavaScript, it's a whole different story. Because the source must be available to the runtime so that it can be parsed and run, the code can always find it's own source file, open it, and print it out.
This was answered here.
The short answer is that you cannot print it via reflection.
If you want to print out the file, then you will need to load in the source file itself (and have the file available).

What does byte& mean? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
//
string productName;
byte& local1 = (byte&) productName;
//
What is this byte&?
I really don't understand. I got code from my friend but I don't understand what this line wants to tell? Because it gives error or redline in my VS2012. Can anyone explain?
It's making local1 a reference byte type, but that's probably coming from some IL decompilation and not valid C# syntax. That result code will more likely happen if you decompile a IL function with a ref string parameter.
You can't compile that code using a C# compiler. There are many things that are legal in IL but there's no C# equivalent syntax, and that's usually the way it's decompiled back (which doesn't make it compilable) in a best-effort to make it look like C#
It is invalid. The closest syntax is byte? - which adds null support to byte.

Categories