| Thread overview |
|---|
February 03, 2014 How to "scope"? | ||||
|---|---|---|---|---|
| ||||
Seeing as the scope keyword is (being?) deprecated, how would you handle something like this:
class Test
{
private:
string str;
this(string str)
{
this.str = str;
}
public:
static Test createFromString(string str)
{
return new Test();
}
}
void main()
{
// at the end of the scope, test is destroyed and memory is freed
scope Test test = Test.createFromString("test");
}
The only solution I can think of is using scope(exit) something like this:
scope(exit) { destroy(test); GC.free(cast(void*)test); }
which seems clumsy. Any other, better solutions?
| ||||
February 03, 2014 Re: How to "scope"? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin | Oops, I of course meant:
static Test createFromString(string str)
{
return new Test(str);
}
| |||
February 03, 2014 Re: How to "scope"? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin | On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote: > Oops, I of course meant: > > static Test createFromString(string str) > { > return new Test(str); > } You _can_ use scoped but it may allocate way to much and it's ugly to use: http://dlang.org/phobos/std_typecons.html#.scoped AFAIK scope'd classes was only deprecated because it _can_ be solved with a library solution and scope is/was not fully implemented. So it was more easy to depecate it and replace it with a library solution, as to implement scope as it stand in the docs. | |||
February 03, 2014 Re: How to "scope"? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Monday, 3 February 2014 at 17:43:09 UTC, Namespace wrote: > On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote: >> Oops, I of course meant: >> >> static Test createFromString(string str) >> { >> return new Test(str); >> } > > You _can_ use scoped but it may allocate way to much and it's ugly to use: > http://dlang.org/phobos/std_typecons.html#.scoped > > AFAIK scope'd classes was only deprecated because it _can_ be solved with a library solution and scope is/was not fully implemented. So it was more easy to depecate it and replace it with a library solution, as to implement scope as it stand in the docs. I'm aware of "scoped", that's why I used this specific example. How do you use scoped on a function that returns a new instance of some object? auto obj = scoped(functionThatReturnsNewObject()); That obviously doesn't work. | |||
February 03, 2014 Re: How to "scope"? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Martin | On Monday, 3 February 2014 at 17:50:33 UTC, Martin wrote:
> On Monday, 3 February 2014 at 17:43:09 UTC, Namespace wrote:
>> On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote:
>>> Oops, I of course meant:
>>>
>>> static Test createFromString(string str)
>>> {
>>> return new Test(str);
>>> }
>>
>> You _can_ use scoped but it may allocate way to much and it's ugly to use:
>> http://dlang.org/phobos/std_typecons.html#.scoped
>>
>> AFAIK scope'd classes was only deprecated because it _can_ be solved with a library solution and scope is/was not fully implemented. So it was more easy to depecate it and replace it with a library solution, as to implement scope as it stand in the docs.
>
> I'm aware of "scoped", that's why I used this specific example. How do you use scoped on a function that returns a new instance of some object?
>
> auto obj = scoped(functionThatReturnsNewObject());
>
> That obviously doesn't work.
In this case where your object already exist and is on the geap, you may want to use Unique:
----
import std.stdio;
import std.typecons;
class Foo {
~this() {
writeln("Foo::DTor");
}
}
Foo createNewFoo() {
return new Foo();
}
void main() {
{
writeln("Startt");
Unique!(Foo) obj = Unique!(Foo)(createNewFoo());
writeln("End");
}
writeln("end of main");
}
----
| |||
February 03, 2014 Re: How to "scope"? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Monday, 3 February 2014 at 17:58:43 UTC, Namespace wrote:
> On Monday, 3 February 2014 at 17:50:33 UTC, Martin wrote:
>> On Monday, 3 February 2014 at 17:43:09 UTC, Namespace wrote:
>>> On Monday, 3 February 2014 at 17:32:07 UTC, Martin wrote:
>>>> Oops, I of course meant:
>>>>
>>>> static Test createFromString(string str)
>>>> {
>>>> return new Test(str);
>>>> }
>>>
>>> You _can_ use scoped but it may allocate way to much and it's ugly to use:
>>> http://dlang.org/phobos/std_typecons.html#.scoped
>>>
>>> AFAIK scope'd classes was only deprecated because it _can_ be solved with a library solution and scope is/was not fully implemented. So it was more easy to depecate it and replace it with a library solution, as to implement scope as it stand in the docs.
>>
>> I'm aware of "scoped", that's why I used this specific example. How do you use scoped on a function that returns a new instance of some object?
>>
>> auto obj = scoped(functionThatReturnsNewObject());
>>
>> That obviously doesn't work.
>
> In this case where your object already exist and is on the geap, you may want to use Unique:
>
> ----
> import std.stdio;
> import std.typecons;
>
> class Foo {
> ~this() {
> writeln("Foo::DTor");
> }
> }
>
> Foo createNewFoo() {
> return new Foo();
> }
>
> void main() {
> {
> writeln("Startt");
> Unique!(Foo) obj = Unique!(Foo)(createNewFoo());
> writeln("End");
> }
>
> writeln("end of main");
> }
> ----
That's exactly what I was looking for. Brilliant, thanks!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply