4 ms·
So these lines are now equivalent: public override string ToString() { return String.Format("({0}, {1})", X, Y); } public override string ToString() => "
by zaroth 12y ago
So these lines are now equivalent:
public override string ToString() { return String.Format("({0}, {1})", X, Y); }
public override string ToString() => "(\{X}, \{Y})"
Could it go even further? This isn't paper after all, on screen why shouldn't it look like;
public override string ToString() => "(X, Y)"
where the X and Y are either slight italics or underlined or colored to indicate they mean X and Y the variable not X and Y the letters. The IDE would let you just toggle the state between 'variable' and 'literal' with the keyboard.
We also get better array initializers:
public JObject ToJson() {
var r = new JObject();
r["x"] = X;
r["y"] = Y;
return r;
}
becomes:
public JObject ToJson() => return new JObject() { ["x"] = X, ["y"] = Y };
That's cool! Next comes the null-conditional operators:
public static Point FromJson(JObject json)
{
if (json != null &&
json["x"] != null &&
json["x"].Type == JTokenType.Integer &&
json["y"] != null &&
json["y"].Type == JTokenType.Integer)
{
return new Point((int)json["x"], (int)json["y"]);
}
return null;
}
Becomes...
public static Point FromJson(JObject json)
{
if (json?["x"]?.Type == JTokenType.Integer &&
json?["y"]?.Type == JTokenType.Integer)
{
return new Point((int)json["x"], (int)json["y"]);
}
return null;
}
The check against JTokenType.Integer is just to avoid a casting exception. The null checks are just to avoid null exceptions. I wonder if the compiler could ever handle something like 'this if you can, else null', without actually having any exceptions getting throw and caught underneath:
public static Point FromJson(JObject json) =>
return new Point((int)json["x"], (int)json["y"]) ?: null;
- coldtea 12y ago>where the X and Y are either slight italics or underlined or colored to indicate they mean X and Y the variable not X and Y the letters. The IDE would let you just toggle the state between 'variable' and 'literal' with the keyboard. A big nope from me. Nicer visual representation is OK. Changing syntax based on visual attributes it's not.
- coldtea 12y ago[Edit: this perhaps should have been "Changing semantics based on visual attributes is not"].
- petercooper 12y agowhere the X and Y are either slight italics or underlined or colored You might find this interesting if you're thinking along those lines: http://joshondesign.com/2014/08/22/typopl http://joshondesign.com/2014/08/22/typopl
- towelguy 12y ago>where the X and Y are either slight italics or underlined or colored to indicate they mean X and Y the variable not X and Y the letters So what happens if you open the file in a different editor?
- hackinthebochs 12y agoWhy are we letting 50 year old technology constrain us? Why should we ever expect to edit code in a bare bones text editor? I'm not saying I like this particular idea but we shouldn't be held back by our technological history
- zaroth 12y agoI guess it could show up with the \{X} escape notation in Notepad. But in theory almost no one would even notice that, the "natural form" would be to eliminate excessive escaping and just show what the string will actually be.
- electrum 12y agoIntelliJ has code folding that can display things using a less verbose syntax. For example, folding of closures (pre-Java 8): http://blog.jetbrains.com/idea/2009/03/closure-folding-in-intellij-idea-9-maia/ http://blog.jetbrains.com/idea/2009/03/closure-folding-in-in...