I am trying to figure out the best way to share a type between files.
Basically this is what I want:
// TranspositionTable.cs
using TranspositionTableEntry = (ulong fullHash, int searchDepth, int refutationTo, int refutationFrom, double eval, NodeType type);
// Ai.cs
using TranspositionTableEntry = TranspositionTable.TranspositionTableEntry
But in Ai.cs I will get an error because TranspositionTableEntry doesn't exist.
I can think of two solutions. First I can change Ai.cs to use the same definition as in TranspositionTable.cs, and then just update it if I change the definition of this tuple.
Otherwise I can just create my own class instead of using ValueTuple. I understand how to import if I want to use a class, but I just want to use a ValueTuple since I want to avoid allocating new space on the heap every time I do that for performance reasons, since I will construct a lot of these entries.
I am trying to figure out the best way to share a type between files.
Basically this is what I want:
// TranspositionTable.cs
using TranspositionTableEntry = (ulong fullHash, int searchDepth, int refutationTo, int refutationFrom, double eval, NodeType type);
// Ai.cs
using TranspositionTableEntry = TranspositionTable.TranspositionTableEntry
But in Ai.cs I will get an error because TranspositionTableEntry doesn't exist.
I can think of two solutions. First I can change Ai.cs to use the same definition as in TranspositionTable.cs, and then just update it if I change the definition of this tuple.
Otherwise I can just create my own class instead of using ValueTuple. I understand how to import if I want to use a class, but I just want to use a ValueTuple since I want to avoid allocating new space on the heap every time I do that for performance reasons, since I will construct a lot of these entries.
Share Improve this question edited Mar 30 at 0:07 Alex Li asked Mar 29 at 23:00 Alex LiAlex Li 2834 silver badges13 bronze badges 2 |3 Answers
Reset to default 3You can use a global using.
global using TranspositionTableEntry = (ulong fullHash, int searchDepth, int refutationTo, int refutationFrom, double eval, NodeType type);
Put this at the very beginning of a file, and you will be able to use the name TranspositionTableEntry
in every other file in your project.
That said, I still recommend that you create your own type. Since you are worried about heap allocations, you can write a (record) struct instead of a class.
record struct TranspositionTableEntry(ulong fullHash, int searchDepth, int refutationTo, int refutationFrom, double eval, NodeType type) {}
This will allow you to get the value tuple from the record as well as set the value tuple to a record.
public record struct TranspositionTableEntry(ulong FullHash, int SearchDepth, int RefutationTo, int RefutationFrom, double Eval, NodeType NodeType)
{
public static implicit operator TranspositionTableEntry((ulong fullHash, int searchDepth, int refutationTo, int refutationFrom, double eval, NodeType nodeType) entry)
{
return new(entry.fullHash, entry.searchDepth, entry.refutationTo, entry.refutationFrom, entry.eval, entry.nodeType);
}
}
You're quite close! but something is missing out. In C#, you can't directly alias a ValueTuple
like this across files using just a using
statement unless the alias is defined in a common, shared scope (like a class or namespace both files can see).
To share a ValueTuple
type across files, you can define the alias inside a static class and reuse it via a using
directive in other files.
To solve this, follow the approach below:
- In a file like
TranspositionTableTypes.cs
, define
namespace TranspositionTable
{
public static class Types
{
public static (ulong fullHash, int searchDepth, int refutationTo, int refutationFrom, double eval, NodeType type) Create(
ulong fullHash, int searchDepth, int refutationTo, int refutationFrom, double eval, NodeType type)
{
return (fullHash, searchDepth, refutationTo, refutationFrom, eval, type);
}
public static readonly (ulong, int, int, int, double, NodeType) Default = default;
}
}
- Then, in
Ai.cs
(or any other file):
using TranspositionTable;
using TranspositionTableEntry = System.ValueTuple<ulong, int, int, int, double, NodeType>;
These should sort you out I believe
record struct
which is very similar – Chronicle Commented Mar 29 at 23:10