Eyrim
Eyrim
CC#
Created by Eyrim on 4/26/2023 in #help
❔ Unable to box custom value type
Hiya, I'm writing a library to interact with MIDI files. I'm laying down some framework stuff (seven bit numbers, Variable Length Quantities [VLQ] etc.), the VLQ's and seven bit numbers are implemented as structs currently.
using System;
using MidiLibrary.Util;

namespace MidiLibrary.Structures
{
public readonly struct VariableLengthQuantity
//TODO: Override ToString
{
private readonly bool[][] Number { get; init; }

private VariableLengthQuantity(long number)
{
bool[] binaryRep = BinaryTools.LongToBinary(number);
bool[][] sevenBitNumbers = BinaryTools.BinaryToSevenBitNumbers(binaryRep);
// Reverse the array
Array.Reverse(sevenBitNumbers, 0, sevenBitNumbers.Length);

for (int i = 0; i < sevenBitNumbers.Length; i++)
{
sevenBitNumbers[i] = BinaryTools.PadBinary(sevenBitNumbers[i], 8);
// everything that isn't next, has a 1 (true) as MSB
sevenBitNumbers[i][^1] = true;
}

sevenBitNumbers[0][^1] = false;

this.Number = sevenBitNumbers;
}

public static explicit operator VariableLengthQuantity(long number) => new VariableLengthQuantity(number);

private static byte[] FromLong(long number)
{
throw new NotImplementedException();
}

private static long ToLong(byte[] array)
{
throw new NotImplementedException();
}
}
}
using System;
using MidiLibrary.Util;

namespace MidiLibrary.Structures
{
public readonly struct VariableLengthQuantity
//TODO: Override ToString
{
private readonly bool[][] Number { get; init; }

private VariableLengthQuantity(long number)
{
bool[] binaryRep = BinaryTools.LongToBinary(number);
bool[][] sevenBitNumbers = BinaryTools.BinaryToSevenBitNumbers(binaryRep);
// Reverse the array
Array.Reverse(sevenBitNumbers, 0, sevenBitNumbers.Length);

for (int i = 0; i < sevenBitNumbers.Length; i++)
{
sevenBitNumbers[i] = BinaryTools.PadBinary(sevenBitNumbers[i], 8);
// everything that isn't next, has a 1 (true) as MSB
sevenBitNumbers[i][^1] = true;
}

sevenBitNumbers[0][^1] = false;

this.Number = sevenBitNumbers;
}

public static explicit operator VariableLengthQuantity(long number) => new VariableLengthQuantity(number);

private static byte[] FromLong(long number)
{
throw new NotImplementedException();
}

private static long ToLong(byte[] array)
{
throw new NotImplementedException();
}
}
}
This is the boxing allocation I'm trying to use:
VariableLengthQuantity a = (VariableLengthQuantity) 137;
object aa = a;
VariableLengthQuantity a = (VariableLengthQuantity) 137;
object aa = a;
Full disclosure, I'm pretty new to using structs. I'd normally just throw it into a class but figure I should probably branch out to something better. Boxing allocation: conversion from 'VariableLengthQuantity' to 'object' requires boxing of value type
8 replies