Is phobos too fluffy?
Jacob Carlborg
doob at me.com
Fri Sep 18 15:13:51 UTC 2020
On 2020-09-17 18:34, H. S. Teoh wrote:
> as opposed to:
>
> // Better
> struct MyRange(E)
> {
> @property bool empty() { return _isEmpty; }
> @property E front() { return _front; }
> void popFront() { r.popFront; }
> }
I hate that style. But I wouldn't mind if D supported expression body
definition like C# does:
struct MyRange(E)
{
@property bool empty() => _isEmpty;
@property E front() => _front;
void popFront() => r.popFront;
}
It would look even better in Scala:
class MyRange[E]
{
def empty = _isEmpty
def front = _front
def popFront() = r.popFront
}
In most languages in the C family, removing the curly braces for the
body of `if`, `for`, `while` and so on is supported if the body only
contains a single statement. Compared to Java, D extended this and
allows to drop the curly braces for `try`, `catch` and `finally` as
well. It just makes sense to allow to drop them for function bodies as well.
Scala goes even further and allows to drop the curly braces for classes:
class Foo
Not a very useful class but something like this also works in Scala:
class Point(x: Int, y: Int)
The above will automatically generate a instance variables,
getters/setters and a constructor for the specified `x` and `y`.
Allowing to drop the curly braces is extra useful because in Scala the
last statement in a method is returned automatically, that in
combination with that all statements are actually expressions:
class Foo
{
def bar(x: Int) =
if (a == 3)
"3"
else if (a == 4)
"4"
else
"other"
}
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list