Replies: 13 comments
-
I like this idea. |
Beta Was this translation helpful? Give feedback.
-
@MovGP0 Why? Do you really think that the following:
Is more clear/readable than this?
What would happen if two libraries define the same operator but they have different semantics for it? What would happen if the designer at some point will introduce the operator |
Beta Was this translation helpful? Give feedback.
-
@eyalsk no I don't. Proposal #170 is mostly to save on typing. Mentioning it was simply my way to give an very easy example. The real usefulness lies in complex scenarios like mathematics, parser generators, SMT solvers, and the like. |
Beta Was this translation helpful? Give feedback.
-
@MovGP0 Can you give me a complex example then that using a method is a blocker? or/and can you answer my questions? |
Beta Was this translation helpful? Give feedback.
-
"Can you give me a complex example then that using a method is a blocker" you can always use a named method instead. the intention behind of having a DSL with custom operators is to improve the expressiveness of the language, which in turn improves readability and reduce typing. "What would happen if two libraries define the same operator but they have different semantics for it?" This is the same issue with every extension method. You would have to give the full namespace: var scaledVector = VectorExtensions.(*) (vector, factor); "What would happen if the designer at some point will introduce the operator There is a difference between And yes, this might confuse people occasionally, but I'm not expecting the language to introduce new operators when you can use extensions instead. |
Beta Was this translation helpful? Give feedback.
-
Improving readability is extremely subjective, you know that. :)
Nop it's not because methods operate on objects and you (the programmer) can understand from the context on what you operate. It is not the same with operators, if you have two operators that have the same syntax but different semantics then mixing them in the same codebase would be cumbersome and confusing.
Yes, I understand but I think that it would be confusing. Just stating my opinion here but I think that many people confuse terseness with readability, if symbols have well defined meaning, I don't mind it and in fact I prefer it but I'm against defining arbitrary symbols, maybe it just me though, dunno. :) |
Beta Was this translation helpful? Give feedback.
-
Kotlin supports something like this called infix For example, kotlin-test has something like this: should("name be equal to Goofy") {
character.name shouldBe "Goofy"
} Where What precedence order should extension operators have? |
Beta Was this translation helpful? Give feedback.
-
Custom operators are a nifty idea, but only if the language has been designed from the ground up to support them. You could try adding them to Roslyn yourself to see the amount of effort that is necessary to support them in the parser and beyond. Even if you take a shortcut and base their precedence on the first character to avoid rewriting the whole syntax tree after obtaining semantic information, it's still a whole lot of work and syntactic ambiguities are still there. Should |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom Does it allow you to add these infix operators as symbols? |
Beta Was this translation helpful? Give feedback.
-
@eyalsk No; they follow the same rules as identifiers. |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox to avoid ambiguity, the expressions would be evaluated from left to right. Assuming that you have the following code: public static class ComplexExtensions
{
public static Complex operator (*) (this Complex left, this Complex right) {
return left * right;
}
public static Complex operator (*) (this Complex number) {
return number.Conjungate();
}
public static Complex operator (~*) (this Complex number) {
return number.Inverse();
}
public static Complex operator (**) (this Complex number, double exponent) {
return number.Pow(exponent);
}
} the expression var r = ComplexExtensions.(**) (a, b); the expression var a1 = ComplexExtensions.(*) (a);
var r = ComplexExtensions.(*) (a1, b); while the expression var b1 = ComplexExtensions.(~*) (b);
var r = ComplexExtensions.(*) (a, b1); |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom named operators aren't the goal here. Existing extension method mechanisms handle those cases just welll: [Then("name should be equal to (.*)")]
public function void NameShouldBeEqualTo(string name)
{
var character = TestContext.Get<Character>();
character.Name.Should().Be(name);
} see also: |
Beta Was this translation helpful? Give feedback.
-
I didn't say it was, just that there is a language with a similar functionality and that the feature is very useful in practice.
I could make the exact same claim about your proposal. |
Beta Was this translation helpful? Give feedback.
-
There should be a way to create custom operator extensions. They are very useful in creating DSLs. Examples:
Often used Methods
See also: #170
usage:
generated code:
Mathematics
Parser Generators
See also: FParsec.Primitives
See also
Beta Was this translation helpful? Give feedback.
All reactions