Draco-lang / Language-suggestions

Collecting ideas for a new .NET language that could replace C#
75 stars 5 forks source link

Operator/infix function `implies` #36

Open WhiteBlackGoose opened 2 years ago

WhiteBlackGoose commented 2 years ago

Problem

Imagine you want to perform the following common pattern: see, that if something is valid, then if its something else is true.

For example, you want to filter out people whose name surely doesn't end with "mia", however some accounts don't have names, and you can't say anything about them:

string? name = ...
if (name is null || name.EndsWith("mia"))
    Exclude(name);

Definition

Syntax

If we're going to do literal logical operators, like and, or, etc., then the syntax should be implies.

Otherwise, it could be ==>, for example.

Meaning

The meaning of A implies B is (not A) or B.

The operator priority of it should be the lowest, even lower than equality: a + b == 0 implies a - b < 0 is (a + b == 0) implies (a - b < 0).

Under the hood

It could be either an operator or infix function. The concern with infix functions is precedence, since implies should have a very low priority.

Examples

1.

string? name = ...
if (name is notnull implies name.EndsWith("mia"))
    return "These aren't the droids you're looking for";

2.

if (input.IsValid implies input.Length > 3)
    ...
yamin8000 commented 2 years ago

however some accounts don't have names

Does that mean name is null for them? I may not fully understand your purpose for this operator/infix function, but is this similar to how safe method calls work in Kotlin? Or maybe safe calls are only subset of this operator functionality?

WhiteBlackGoose commented 2 years ago

Does that mean name is null for them?

Yes

I may not fully understand your purpose for this operator/infix function, but is this similar to how safe method calls work in Kotlin?

Not really, though monadic nullables is imo a must for Fresh.

It's basically just a boolean operator, like and/&& and things, it's just its truth table looks like a b and or implies
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 1
pxeger commented 2 years ago

I don't see any point in this, when you can just use (not a) or b which is clearer and doesn't add an extra feature to maintain. It just adds extra cognitive load, and one more thing new users have to learn.