akaJB
akaJB
CC#
Created by akaJB on 7/30/2023 in #help
✅ Confusion over defining a default implementation on an interface
I have an interface I'll call IWidget, and a class which implements it called Widget. I created a property IWidget with a default implementation. However, I cannot access that property on a Widget instance. My expectation was that any class/interface which implements IWidget would get its default implementation. When looking up some of the docs online I see this is expected behavior and has something to do with preventing breaking changes. What I'm not finding is answers to two questions: - What's the benefit of providing an implementation to begin with if it's not direclty accessable? - What's the best pattern to follow if there's shared behavior across all implementations of IWidget? I would assume an extension for IWidget but would like to confirm Fuller example:
using System;

namespace Test
{
public interface IWidget
{
public string GetName { get; }

public int GetNumber => 1;
}

public class Widget : IWidget
{
public string GetName => "Widget";
}

public static class Program
{
public static void Main(string[] args)
{
Widget widget = new Widget();
IWidget iwidget = widget;

// both work
Console.WriteLine(widget.GetName);
Console.WriteLine(iwidget.GetName);

// widget has an error
// iwidget works
Console.WriteLine(widget.GetNumber);
Console.WriteLine(iwidget.GetNumber);
}
}
}
using System;

namespace Test
{
public interface IWidget
{
public string GetName { get; }

public int GetNumber => 1;
}

public class Widget : IWidget
{
public string GetName => "Widget";
}

public static class Program
{
public static void Main(string[] args)
{
Widget widget = new Widget();
IWidget iwidget = widget;

// both work
Console.WriteLine(widget.GetName);
Console.WriteLine(iwidget.GetName);

// widget has an error
// iwidget works
Console.WriteLine(widget.GetNumber);
Console.WriteLine(iwidget.GetNumber);
}
}
}
6 replies
CC#
Created by akaJB on 7/29/2023 in #help
✅ How do I deal with generic covariance and collections types?
Let's say I have osmething like the following:
public interface IBoxCollection<out T> : IBox
where T : IBox
{
public Dictionary<string, T> Collection { get; }
}
public interface IBoxCollection<out T> : IBox
where T : IBox
{
public Dictionary<string, T> Collection { get; }
}
I get the following error: Invalid variance: The type parameter 'T' must be invariantly valid on 'IBoxCollection<T>.Collection'. 'T' is covariant I've looked around on how to resolve this issue but haven't found anything. Is there an easy solution to this problem, or is there a pattern which will accomplish what I'm looking for?
8 replies
CC#
Created by akaJB on 7/28/2023 in #help
❔ Attempting to create a Result<T, E> type, running into issues with subclasses
I want to create a Result<T, E> and I'm running into trouble with the following: let's say I have a class Foo and a subclass Bar : Foo. If I create an instance of Result<Bar, _> I had expected it to be assignable to Result<Foo, _> but that doesn't seem to be the case. For a fuller example see: https://dotnetfiddle.net/UEgC3P Is there a way that allows such an assignment? Else how can I ergonomically work with the compiler? I could of course create a new Result instance (like the below), but it's very verbose.
return result.IsSuccess
? Result<Foo, int>.AsOk(result.Value)
: Result<Foo, int>.AsError(result.Error);
return result.IsSuccess
? Result<Foo, int>.AsOk(result.Value)
: Result<Foo, int>.AsError(result.Error);
9 replies