隐式转换没有分配? [英] Implicit conversion without assignment?

查看:155
本文介绍了隐式转换没有分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保留的问题 - 见底部修改

我工作的一个小功能库,基本上可以通过隐藏基本圈的复杂性提供了一些可读性。提供者被称为选择< T> (与助手工厂名为选择)和用法类似于

 公众的Guid? GetPropertyId(...)
{
返回选择
.Either(TryToGetTheId(...))
。或(TrySomethingElseToGetTheId(...))
。或(IGuessWeCanTryThisTooIfWeReallyHaveTo(...))
//等。
;
}

和我还增加了图书馆将照顾短路的等。 T> 到 T ,这样我就可以写


$从选择℃的隐式转换b $ b

 公众的Guid GetPropertyId(...)
{
ServiceResult结果=选择
.Either(TryToGetTheId(...))
。或(TrySomethingElseToGetTheId(...));

返回result.Id;
}



我真的希望能够做一个隐式转换ŧ没有分配:

 公众的Guid GetPropertyId(...)
{
返回
//这是我想被隐式转换为一个ServiceResult
选择
.Either(TryToGetTheId(...))
。或(TrySomethingElseToGetTheId(...))的一部分
//然后我想就投
.ID的结果访问此属性;
}



不过,指定的语法不行 - 我要么给它分配一个变量,或者明确地投放。有没有办法得到一个隐式转换内联?



修改



什么我想要做的是这样的:

 类Foo {
公众诠释FUH {搞定;组; }
}

类酒吧{
私人美孚_foo;
公共静态隐运营商美孚(酒吧酒吧)
{
返回bar._foo;
}
}

//我要做的
富巴= GetABar();
DoSomethingWith(bar.Fuh);

//我想这样做
DoSomethingWith(GetABar()FUH。)什么;


解决方案

虽然这是事实,隐式转换将无法正常工作在这里,你可能做的加入了属性选择<不是一个明确的转换更好; T> 。然后,你的表情会是选择。[操作] .Value.Id ,仍读得相当好。


Preserved question - see Edit at the bottom
I'm working on a small functional library, basically to provide some readability by hiding basic cyclomatic complexities. The provider is called Select<T> (with a helper factory called Select), and usage is similar to

public Guid? GetPropertyId(...)
{
    return Select
        .Either(TryToGetTheId(...))
        .Or(TrySomethingElseToGetTheId(...))
        .Or(IGuessWeCanTryThisTooIfWeReallyHaveTo(...))
        //etc.
        ;
}

and the library will take care of the short circuiting, etc. I also added an implicit conversion from Select<T> to T, so I can write

public Guid GetPropertyId(...)
{
    ServiceResult result = Select
        .Either(TryToGetTheId(...))
        .Or(TrySomethingElseToGetTheId(...));

    return result.Id;
}

What I'd really like to be able to do is an implicit conversion to T without assignment:

public Guid GetPropertyId(...)
{
    return 
        //This is the part that I want to be implicitly cast to a ServiceResult
        Select
        .Either(TryToGetTheId(...))
        .Or(TrySomethingElseToGetTheId(...))
        //Then I want to access this property on the result of the cast
        .Id;
}

However, the specified syntax doesn't work - I have to either assign it to a variable, or explicitly cast it. Is there a way to get an implicit cast inline?

EDIT

What I want to do is this:

class Foo { 
    public int Fuh { get; set; } 
}

class Bar {
    private Foo _foo;
    public static implicit operator Foo (Bar bar)
    {
        return bar._foo;
    }
}

//What I have to do
Foo bar = GetABar();
DoSomethingWith(bar.Fuh);

//What I want to do
DoSomethingWith(GetABar().Fuh);

解决方案

While it's true that an implicit cast won't work here, you could possibly do better than an explicit cast by adding a Value property to Select<T>. Then your expression would be Select.[operations].Value.Id, which still reads reasonably well.

这篇关于隐式转换没有分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆