我可以在 C# 中创建三元运算符吗?

发布于 2024-08-20 12:38:00 字数 128 浏览 8 评论 0原文

我想为 < 创建一个三元运算符b< c 是 a <乙&& b< c.或您能想到的任何其他选项 < b> c等等...我是我自己的短片的粉丝,自从我在高中学习编程以来我就想创建它。

如何?

I want to create a ternary operator for a < b < c which is a < b && b < c. or any other option you can think of that a < b > c and so on... I am a fan of my own shortform and I have wanted to create that since I learned programming in high school.

How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

潦草背影 2024-08-27 12:38:00

不要相信那些讨厌的人;)

可以在 C# 中做到这一点。这是一个示例实现 - 我基于 Icon 的方式进行链接...如果比较成功,结果是正确的参数,否则返回特殊的“失败”结果。

您需要使用的唯一额外语法是在第一项之后调用 Chain()

class Program
{
    static void Main(string[] args)
    {
        if (2.Chain() < 3 < 4)
        {
            Console.WriteLine("Yay!");
        }
    }
}

public class Chainable<T> where T : IComparable<T>
{
    public Chainable(T value)
    {
        Value = value;
        Failed = false;
    }

    public Chainable()
    {
        Failed = true;
    }

    public readonly T Value;
    public readonly bool Failed;

    public static Chainable<T> Fail { get { return new Chainable<T>(); } }

    public static Chainable<T> operator <(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == -1
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator >(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == 1
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator ==(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == 0
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator !=(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) != 0
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static bool operator true(Chainable<T> me)
    {
        return !me.Failed;
    }

    public static bool operator false(Chainable<T> me)
    {
        return me.Failed;
    }

    public override bool Equals(object obj)
    {
        return Value.Equals(obj);
    }

    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }
}

public static class ChainExt
{
    public static Chainable<T> Chain<T>(this T value) where T : IComparable<T>
    {
        return new Chainable<T>(value);
    }
}

Don't believe the haters ;)

You can do this in C#. Here's an example implementation — I based the chaining off the way that Icon does theirs... if a comparison succeeds, the result is that of the right parameter, otherwise a special "failed" result is returned.

The only extra syntax you need to use is the call to Chain() after the first item.

class Program
{
    static void Main(string[] args)
    {
        if (2.Chain() < 3 < 4)
        {
            Console.WriteLine("Yay!");
        }
    }
}

public class Chainable<T> where T : IComparable<T>
{
    public Chainable(T value)
    {
        Value = value;
        Failed = false;
    }

    public Chainable()
    {
        Failed = true;
    }

    public readonly T Value;
    public readonly bool Failed;

    public static Chainable<T> Fail { get { return new Chainable<T>(); } }

    public static Chainable<T> operator <(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == -1
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator >(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == 1
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator ==(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) == 0
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static Chainable<T> operator !=(Chainable<T> me, T other)
    {
        if (me.Failed)
            return Fail;

        return me.Value.CompareTo(other) != 0
                   ? new Chainable<T>(other)
                   : Fail;
    }

    public static bool operator true(Chainable<T> me)
    {
        return !me.Failed;
    }

    public static bool operator false(Chainable<T> me)
    {
        return me.Failed;
    }

    public override bool Equals(object obj)
    {
        return Value.Equals(obj);
    }

    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }
}

public static class ChainExt
{
    public static Chainable<T> Chain<T>(this T value) where T : IComparable<T>
    {
        return new Chainable<T>(value);
    }
}
瑾兮 2024-08-27 12:38:00

抱歉,您无法在 C# 中创建自己的运算符。

您可以使用扩展方法来启用流畅的语法,例如

bool f = b.IsBetween(a, c);

或者,如果您非常聪明,您可以这样做:

bool f = a.IsLessThan(b).IsLessThan(c);

这样做很棘手,但可能。 (提示:定义 IsLessThan 返回的自定义对象,该对象跟踪其边界并了解它如何与该对象的其他实例组合。本质上,这就是 LINQ-to-SQL 结合Where、Select 等的工作方式。)

但您无法在 C# 中定义自己的运算符语法。

如果您对可以定义自己的运算符的语言感兴趣,您可以考虑研究 F#。

Sorry, you cannot create your own operators in C#.

You could use extension methods to enable a fluent syntax like

bool f = b.IsBetween(a, c);

Or, if you were being extremely clever, you could do:

bool f = a.IsLessThan(b).IsLessThan(c);

doing so is tricky, but possible. (Hint: define a custom object that IsLessThan returns that tracks its bounds and understands how it is combined with other instances of the object. Essentially this is how LINQ-to-SQL works with regard to combining Where, Select, and so on.)

But you cannot define your own operator syntaxes in C#.

If you are interested in languages where you can define your own operators, you might consider looking into F#.

夏末染殇 2024-08-27 12:38:00

你不能那样做。您只能实现现有的运算符,并且没有三元 .<.<。 C# 中的运算符。

此外,这样的运算符与现有的比较运算符会产生歧义。例如,表达式 a == b == c == d 是否意味着 ((a == b) == c) == d( a == b == c) == d

You can't do that. You can only implement existing operators, and there is no ternary .<.<. operator in C#.

Besides, such an operator would be ambiguous with existing comparing operators. For example, would the expression a == b == c == d mean ((a == b) == c) == d or (a == b == c) == d?

看透却不说透 2024-08-27 12:38:00

您是否尝试过在谷歌中搜索三元运算符以查看是否已经存在某些内容?

Have you tried searching for ternary operators in google to see if something already exists?

脱离于你 2024-08-27 12:38:00

不,C# 中没有。这种方式的语言扩展在任何当前版本的 C# 中都不可用,但 Luca Bolognese 指出使用编译器作为服务和一些功能,可以允许以这种方式扩展语言:http://microsoftpdc.com/Sessions/FT11

No. Not in C#. Language extension in this way is not available in any current version of C#, but Luca Bolognese pointed to using the compiler as a service and some functionality which could permit extending the language in such a way here: http://microsoftpdc.com/Sessions/FT11.

季末如歌 2024-08-27 12:38:00

如果您想对原始数据类型执行此操作,那么您就不走运了。 C# 不支持向其中添加运算符。

在您自己的数据类型中,可以返回存储中间结果和比较结果的特殊数据类型。但是,我建议您坚持使用 c# 语言 - 如果您确实想要 a a a a a a a a a < b< c 运算符风格,切换到 Python。

If you want to do that for the primitive datatypes, you're out of luck. C# doesn't support adding operators to those.

In your own datatypes it is possible to return a special datatype which stores the intermediate result and the comparisson result. However, I suggest you just stick to the c# language - if you really want the a < b < c operator style, switch to Python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文