Re: Can Scope Be Used for Anonymous Objects?

2018-10-17 Thread Stanislav Blinov via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 20:53:02 UTC, Vijay Nayar wrote:

This particular use of "scope" I overheard at the last DConf, 
and I believe it has been added to the official documentation 
here:  https://dlang.org/spec/expression.html#new_expressions


If a NewExpression is used as an initializer for a function 
local variable with
scope storage class, and the ArgumentList to new is empty, 
then the instance is
allocated on the stack rather than the heap or using the 
class specific allocator.


Class-specific allocators (aka overriding 'new') were deprecated 
a long time ago, and this particular use of 'scope' should 
follow. Unfortunately, as with many other things in D, 
allocators, 'scope', DIP1000 etc are somewhere between here and 
there...


I didn't know about the std.typecons scoped, it looks really 
useful, especially when you want to only create the object when 
short-circuiting fails, like in the middle of an "||" 
expression.


One drawback of "scoped", however, is that it doesn't appear to 
warn you if you accidentally let the reference escape out of 
the function, unlike a scope variable.


That's an artifact of it's implementation predating DIP25 and 
DIP1000, and those DIPs themselves not being realized fully.



Why, exactly, is a trivial thing like this even a class?


Porting C++ code which unfortunately makes heavy use of 
inheritance.  I originally had this as a struct until I much 
later stumbled into the other classes that were inheriting from 
it.


Ouch. If there aren't any virtual functions, you could "inherit" 
via struct inclusion and alias this. Or just take artistic... 
err... porting license and deviate from the original 
implementation :)


Re: Can Scope Be Used for Anonymous Objects?

2018-10-17 Thread Ali Çehreli via Digitalmars-d-learn

On 10/17/2018 01:53 PM, Vijay Nayar wrote:

> https://dlang.org/spec/expression.html#new_expressions
>
>  If a NewExpression is used as an initializer for a function local
> variable with
>  scope storage class, and the ArgumentList to new is empty, then the
> instance is
>  allocated on the stack rather than the heap or using the class
> specific allocator.

I did not know that. It looks like it's a feature for classes only.

Ali



Re: Can Scope Be Used for Anonymous Objects?

2018-10-17 Thread Vijay Nayar via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 20:41:24 UTC, Ali Çehreli wrote:

On 10/17/2018 01:24 PM, Vijay Nayar wrote:

I have a snippet of code like this:
     scope chordAngle = new S1ChordAngle(_center, 
other._center);

     return _radius + other._radius >= chordAngle;

The reason the "scope" temporary variable exists is to avoid a 
heap allocation and instead prefer a value be created on the 
stack.  Is there a way to do this inline?


Something like:
     return _radius + other._radius >= scope new 
S1ChordAngle(_center, other._center);


I think it's possible but what you're looking for is 
std.typecons.scoped. 'scope' does not do what you think it does.


import std.typecons : scoped;

class C {
int i;
this(int i) {
this.i = i;
}
int foo() {
return i;
}
}

bool bar() {

auto c = scoped!C(42);
return 42 == c.foo();
}

bool bar_2() {
return 42 == scoped!C(42).foo();
}

void main() {
bar();
bar_2();
}

Ali


This particular use of "scope" I overheard at the last DConf, and 
I believe it has been added to the official documentation here:  
https://dlang.org/spec/expression.html#new_expressions


If a NewExpression is used as an initializer for a function 
local variable with
scope storage class, and the ArgumentList to new is empty, 
then the instance is
allocated on the stack rather than the heap or using the 
class specific allocator.


I didn't know about the std.typecons scoped, it looks really 
useful, especially when you want to only create the object when 
short-circuiting fails, like in the middle of an "||" expression.


One drawback of "scoped", however, is that it doesn't appear to 
warn you if you accidentally let the reference escape out of the 
function, unlike a scope variable.


Re: Can Scope Be Used for Anonymous Objects?

2018-10-17 Thread Vijay Nayar via Digitalmars-d-learn
On Wednesday, 17 October 2018 at 20:51:29 UTC, Stanislav Blinov 
wrote:
On Wednesday, 17 October 2018 at 20:24:56 UTC, Vijay Nayar 
wrote:

I have a snippet of code like this:
scope chordAngle = new S1ChordAngle(_center, 
other._center);

return _radius + other._radius >= chordAngle;

The reason the "scope" temporary variable exists is to avoid a 
heap allocation and instead prefer a value be created on the 
stack.  Is there a way to do this inline?


Why, exactly, is a trivial thing like this even a class?


Porting C++ code which unfortunately makes heavy use of 
inheritance.  I originally had this as a struct until I much 
later stumbled into the other classes that were inheriting from 
it.


Re: Can Scope Be Used for Anonymous Objects?

2018-10-17 Thread Stanislav Blinov via Digitalmars-d-learn

On Wednesday, 17 October 2018 at 20:24:56 UTC, Vijay Nayar wrote:

I have a snippet of code like this:
scope chordAngle = new S1ChordAngle(_center, other._center);
return _radius + other._radius >= chordAngle;

The reason the "scope" temporary variable exists is to avoid a 
heap allocation and instead prefer a value be created on the 
stack.  Is there a way to do this inline?


Why, exactly, is a trivial thing like this even a class?



Can Scope Be Used for Anonymous Objects?

2018-10-17 Thread Vijay Nayar via Digitalmars-d-learn

I have a snippet of code like this:
scope chordAngle = new S1ChordAngle(_center, other._center);
return _radius + other._radius >= chordAngle;

The reason the "scope" temporary variable exists is to avoid a 
heap allocation and instead prefer a value be created on the 
stack.  Is there a way to do this inline?


Something like:
return _radius + other._radius >= scope new 
S1ChordAngle(_center, other._center);


Re: Can Scope Be Used for Anonymous Objects?

2018-10-17 Thread Ali Çehreli via Digitalmars-d-learn

On 10/17/2018 01:24 PM, Vijay Nayar wrote:

I have a snippet of code like this:
     scope chordAngle = new S1ChordAngle(_center, other._center);
     return _radius + other._radius >= chordAngle;

The reason the "scope" temporary variable exists is to avoid a heap 
allocation and instead prefer a value be created on the stack.  Is there 
a way to do this inline?


Something like:
     return _radius + other._radius >= scope new S1ChordAngle(_center, 
other._center);


I think it's possible but what you're looking for is 
std.typecons.scoped. 'scope' does not do what you think it does.


import std.typecons : scoped;

class C {
int i;
this(int i) {
this.i = i;
}
int foo() {
return i;
}
}

bool bar() {

auto c = scoped!C(42);
return 42 == c.foo();
}

bool bar_2() {
return 42 == scoped!C(42).foo();
}

void main() {
bar();
bar_2();
}

Ali