Andreas-Dorfer / base-types

Fight primitive obsession and create expressive domain models with source generators.
MIT License
18 stars 2 forks source link

[Feature Request] ByteArray (byte[]) or BitArray) implementation #11

Open jeffward01 opened 1 year ago

jeffward01 commented 1 year ago

Hello!

I was working on a PR myself for this, but I have been stumped since byte[] and BitArray do not implement the IComparable interface.

You know your library much better than I do - do you have any advice how an implementation for this can be done?

My thoughts are to:

What do you think? Any advice? Im struggling with the wrapper type being convertible to your IBaseType in the code

Can you point me in the right direction with solving this?


public class BitArrayComparable : IComparable<byte[]>

{
    private readonly byte[] _bitArray;

    public BitArrayComparable(byte[] bitArray)
    {
        this._bitArray = bitArray;
    }

    /// <inheritdoc />
    public int CompareTo(byte[]? other)
    {
        var len = Math.Min(this._bitArray.Length, other.Length);
        for (var i = 0; i < len; i++)
        {
            var c = this._bitArray[i]
                .CompareTo(other[i]);
            if (c != 0)
            {
                return c;
            }
        }

        return other.Length.CompareTo(other.Length);
    }
} 

I had something like this, but its not convertible to IBaseType

Thanks

Andreas-Dorfer commented 1 year ago

That's an open issue #15. The generated base type should only implement IComparable when the wrapped type implements IComparable. I already did an exploration on that but didn't finish it yet (see branch feature/IComparable).

That means, there should be no IComparable implementation for Array / BitArray.

That would fix your issue, right?

jeffward01 commented 1 year ago

I don't know if that will work. The issue is that byte can implement IComparable, but with byte[] it is an array, rather than a value type like int - so we'd need to also add the ability in some capacity to add custom implementations of the IComparable<> interface like I showed above.

I think perhaps we are saying the same thing?