C
C#2y ago
Eyrim

❔ 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
5 Replies
Eyrim
EyrimOP2y ago
(also goes without saying this isn't refactored or anything yet, I literally just wrote it)
basically, i am little cat
Probably something related to (customized or modified) compiler/framework/plugin/whatever. https://discord.com/channels/143867839282020352/251386218351165441/1100797762901131376 Here it compiles fine
MODiX
MODiX2y ago
basically, i am little cat#5356
REPL Result: Success
public readonly struct Foo {
private readonly bool[][] bar;
}

var a = default(Foo);
object b = a;
b
public readonly struct Foo {
private readonly bool[][] bar;
}

var a = default(Foo);
object b = a;
b
Result: Foo
{}
{}
Quoted by
<@!509301565694279680> from #bot-spam (click here)
Compile: 481.937ms | Execution: 30.878ms | React with ❌ to remove this embed.
Eyrim
EyrimOP2y ago
huh, weird because i was thinking like "this is fine, it's boxed, stop complaining" but yeah that makes sense that it's something in my env tysm <33
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server