I have a struct with two const fields: width and height.
But I cannot pass an instance of this struct to a function without knowing width and height.
fn board(comptime width: u8, comptime height: u8) type
{
return struct
{
// code + fields
}
}
I know the Allocator interface does a trick like that, but can’t wrap my head around it.
fn do_something_with_board(board: *const AnyBoard) // how?
{
}
There are two options to do this generally: Either you pass the board as anytype, or you pass the board type in a separate parameter:
fn do_something_with_board(board: anytype)
fn do_something_with_board(BoardType: type, board: *const BoardType)
The second one is probably preferable here, since with the first it is now possible to pass the board as a mutable pointer or by value, generating a new instance of the function for each possible type.
There are also other options if you are willing to do some restructuring. E.g. you could for example just turn this into a member function of Board
to avoid any trouble regarding the type.
Yes I can have member functions. These are no problem.
The trouble is I have to pass the *const Board to several other functions (printing, the move-generator etc.)
passing 2 parameters is quite inconvenient.
Restructuring is no problem, as long as the comptimes stay comptime.
And I like typed. Preventing anytype is almost a must.