• 4 Posts
  • 196 Comments
Joined 1 year ago
cake
Cake day: July 3rd, 2023

help-circle










  • The example is simplified, but I dislike returning null in my own code. The function will always execute, left or right doesn’t matter it’s mapped across in the ResultObject class.

    The function must return an IResult, the ResultObject analyzes the IResult checking for IFail or IOk. If it’s IOk the value of type T is retrieved from the Value property of the IOk object and returned, the Some property defaults to true. If the IResult is an IFail, Some is set to false, it copies the message from the IFail object into the ResultObject, and returns the value the was supplied to its constructor.

    I’m just sharing something I find useful, and I hope I can make it useful for others as well. Thanks for the questions.



  • Here’s a real world side project example of how I handle this situation:

     public IResult GetResourceValue(string path)
        {
            string err = $"{typeof(T).FullName} is not available from this Resource ({nameof(FileSystemResource)})";
    
            switch (typeof(T))
            {
                case Type t when t == typeof(DriveInfo):
                    return (IResult)new Ok(BackingValue);
                case Type t when t == typeof(DirectoryInfo):
                    err = $"Directory path invalid: {path}";
                    var dir = new DirectoryInfo(path);
    
                    if (dir.Exists)
                        return (IResult)new Ok(dir);
                    break;
                case Type t when t == typeof(FileInfo):
                    err = $"File path invalid: {path}";
                    var file = new FileInfo(path);
    
                    if (file.Exists)
                        return (IResult)new Ok(file);
                    break;
            }
            return new Error(err);
        }
    

    You said elsewhere that it feels like you’re doing something wrong if you have to check for every type just to use a Generic. I think you’re right in thinking along those lines. There should be a minimal number of types to check, and Ideally limited via a type constraint.

    Here’s example that includes a constraint:

        public IResult GetValue() where T : struct =>
            typeof(T) switch
            {
                Type t when t == typeof(int) && value <= int.MaxValue =>
                 (IResult)new Ok((int)value),
                Type t when t == typeof(uint) && value <= uint.MaxValue && value >= uint.MinValue =>
                 (IResult)new Ok((uint)value),
                Type t when t == typeof(byte) && value <= byte.MaxValue && value >= byte.MinValue =>
                 (IResult)new Ok((byte)value),
                Type t when t == typeof(sbyte) && value <= (int)sbyte.MaxValue =>
                 (IResult)new Ok((sbyte)value),
                Type t when t == typeof(short) && value <= (int)short.MaxValue =>
                 (IResult)new Ok((short)value),
                Type t when t == typeof(ushort) && value <= ushort.MaxValue =>
                 (IResult)new Ok((ushort)value),
                Type t when t == typeof(long) && value <= long.MaxValue =>
                 (IResult)new Ok((long)value),
                Type t when t == typeof(ulong) => (IResult)new Ok((int)value),
                _ => new IntegerError()
            };