Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 09, 2015 Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
I often have code like this: class A { final: nothrow: ... some methods ... } Problem comes when I need add methods which for eg.: throws or need to be virtual. I can put them before final: but this is not perfect, because I prefer when methods are place in specific order (method abc call method asd so asd is bellow abc and so on). So today I download dmd source and make some small modification (only few lines) and this is the result: http://dpaste.dzfl.pl/472afc938397 |
January 09, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak Attachments: | On Fri, 09 Jan 2015 11:57:50 +0000
Daniel Kozak via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> I often have code like this:
>
> class A {
> final:
> nothrow:
> ...
> some methods
> ...
> }
>
> Problem comes when I need add methods which for eg.: throws or need to be virtual.
>
> I can put them before final: but this is not perfect, because I prefer when methods are place in specific order (method abc call method asd so asd is bellow abc and so on).
>
> So today I download dmd source and make some small modification (only few lines) and this is the result:
>
> http://dpaste.dzfl.pl/472afc938397
i've dreamt about similar feature for a long time! i was thinking about "negative attributes" though (@canthrow, @virtual, etc), but your solution seems to be better as it's not poluting language with new attrs.
can it be used like this:
final:
nothrow:
...
default void foo () { ... }
...
so only `foo` becomes default, but all other methods after `foo` are `final nothrow`?
and can it be used like this:
default void foo () nothrow { ... }
so `default` resets all attrs and then i can specify another set of attrs inline?
|
January 09, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
V Fri, 9 Jan 2015 14:11:00 +0200 ketmar via Digitalmars-d <digitalmars-d@puremagic.com> napsáno: > can it be used like this: > > final: > nothrow: > ... > default void foo () { ... } > ... > > so only `foo` becomes default, but all other methods after `foo` are `final nothrow`? not now, but I plan add this for now you can use: default { void foo () { ... }} > > and can it be used like this: > > default void foo () nothrow { ... } > > so `default` resets all attrs and then i can specify another set of attrs inline? same as the first one but this works: default { void foo () nothrow { ... }} |
January 09, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On 1/9/15 6:57 AM, Daniel Kozak wrote:
> I often have code like this:
>
> class A {
> final:
> nothrow:
> ...
> some methods
> ...
> }
>
> Problem comes when I need add methods which for eg.: throws or need to
> be virtual.
>
> I can put them before final: but this is not perfect, because I prefer
> when methods are place in specific order (method abc call method asd so
> asd is bellow abc and so on).
>
> So today I download dmd source and make some small modification (only
> few lines) and this is the result:
>
> http://dpaste.dzfl.pl/472afc938397
Nice, but I don't like the fact that it bluntly returns all attributes to default.
For example, if you need to remove the final attribute, but not nothrow, I'm assuming it looks something like:
default nothrow void foo()
which doesn't read very well.
If we are going to do this, I'd rather see something like has been suggested before -- parameterizing attributes:
final(false) -> remove final
This allows compile-time booleans to modify attributes in ways that are extremely difficult in templates today.
-Steve
|
January 09, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Attachments: | On Fri, 9 Jan 2015 13:16:48 +0100
Daniel Kozák via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> V Fri, 9 Jan 2015 14:11:00 +0200
> ketmar via Digitalmars-d <digitalmars-d@puremagic.com> napsáno:
>
> > can it be used like this:
> >
> > final:
> > nothrow:
> > ...
> > default void foo () { ... }
> > ...
> >
> > so only `foo` becomes default, but all other methods after `foo` are `final nothrow`?
>
> not now, but I plan add this
> for now you can use:
> default { void foo () { ... }}
>
> >
> > and can it be used like this:
> >
> > default void foo () nothrow { ... }
> >
> > so `default` resets all attrs and then i can specify another set of attrs inline?
>
> same as the first one but this works:
>
> default { void foo () nothrow { ... }}
i think it's acceptable even in this form. please, please make a PR from it, so even if it will not be accepted to mainline, i still can steal it for my private builds! ;-)
|
January 09, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | V Fri, 09 Jan 2015 07:21:02 -0500
Steven Schveighoffer via Digitalmars-d <digitalmars-d@puremagic.com>
napsáno:
> On 1/9/15 6:57 AM, Daniel Kozak wrote:
> > I often have code like this:
> >
> > class A {
> > final:
> > nothrow:
> > ...
> > some methods
> > ...
> > }
> >
> > Problem comes when I need add methods which for eg.: throws or need to be virtual.
> >
> > I can put them before final: but this is not perfect, because I prefer when methods are place in specific order (method abc call method asd so asd is bellow abc and so on).
> >
> > So today I download dmd source and make some small modification (only few lines) and this is the result:
> >
> > http://dpaste.dzfl.pl/472afc938397
>
> Nice, but I don't like the fact that it bluntly returns all attributes to default.
>
> For example, if you need to remove the final attribute, but not nothrow, I'm assuming it looks something like:
>
> default nothrow void foo()
>
> which doesn't read very well.
>
> If we are going to do this, I'd rather see something like has been suggested before -- parameterizing attributes:
>
> final(false) -> remove final
>
> This allows compile-time booleans to modify attributes in ways that are extremely difficult in templates today.
>
> -Steve
I think both ways shoud be available
when I need remove just one attr than attr!false is nice but when I need remove for eg: 3 or more it will look like:
void someFunc final(false) pure(false) nothrow(false)
in this case I think that:
default void foo() nothrow looks OK now :)
|
January 09, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | On 1/9/15 7:47 AM, Daniel Kozák via Digitalmars-d wrote:
> V Fri, 09 Jan 2015 07:21:02 -0500
> Steven Schveighoffer via Digitalmars-d <digitalmars-d@puremagic.com>
> napsáno:
>
>> On 1/9/15 6:57 AM, Daniel Kozak wrote:
>>> I often have code like this:
>>>
>>> class A {
>>> final:
>>> nothrow:
>>> ...
>>> some methods
>>> ...
>>> }
>>>
>>> Problem comes when I need add methods which for eg.: throws or need
>>> to be virtual.
>>>
>>> I can put them before final: but this is not perfect, because I
>>> prefer when methods are place in specific order (method abc call
>>> method asd so asd is bellow abc and so on).
>>>
>>> So today I download dmd source and make some small modification
>>> (only few lines) and this is the result:
>>>
>>> http://dpaste.dzfl.pl/472afc938397
>>
>> Nice, but I don't like the fact that it bluntly returns all
>> attributes to default.
>>
>> For example, if you need to remove the final attribute, but not
>> nothrow, I'm assuming it looks something like:
>>
>> default nothrow void foo()
>>
>> which doesn't read very well.
>>
>> If we are going to do this, I'd rather see something like has been
>> suggested before -- parameterizing attributes:
>>
>> final(false) -> remove final
>>
>> This allows compile-time booleans to modify attributes in ways that
>> are extremely difficult in templates today.
>>
>
> I think both ways shoud be available
>
> when I need remove just one attr than attr!false is nice but when I
> need remove for eg: 3 or more it will look like:
>
> void someFunc final(false) pure(false) nothrow(false)
>
> in this case I think that:
>
> default void foo() nothrow looks OK now :)
>
I'd rather combine the parameterization with attribute sets (i.e. aliasing sets of attributes to one symbol). The 'default' idea is ok, but I think it's too clever -- the word "default" doesn't immediately say "attributes", so it's going to confuse people.
-Steve
|
January 09, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 9 January 2015 at 13:01:14 UTC, Steven Schveighoffer wrote:
> On 1/9/15 7:47 AM, Daniel Kozák via Digitalmars-d wrote:
>> V Fri, 09 Jan 2015 07:21:02 -0500
>> Steven Schveighoffer via Digitalmars-d <digitalmars-d@puremagic.com>
>> napsáno:
>>
>>> On 1/9/15 6:57 AM, Daniel Kozak wrote:
>>>> I often have code like this:
>>>>
>>>> class A {
>>>> final:
>>>> nothrow:
>>>> ...
>>>> some methods
>>>> ...
>>>> }
>>>>
>>>> Problem comes when I need add methods which for eg.: throws or need
>>>> to be virtual.
>>>>
>>>> I can put them before final: but this is not perfect, because I
>>>> prefer when methods are place in specific order (method abc call
>>>> method asd so asd is bellow abc and so on).
>>>>
>>>> So today I download dmd source and make some small modification
>>>> (only few lines) and this is the result:
>>>>
>>>> http://dpaste.dzfl.pl/472afc938397
>>>
>>> Nice, but I don't like the fact that it bluntly returns all
>>> attributes to default.
>>>
>>> For example, if you need to remove the final attribute, but not
>>> nothrow, I'm assuming it looks something like:
>>>
>>> default nothrow void foo()
>>>
>>> which doesn't read very well.
>>>
>>> If we are going to do this, I'd rather see something like has been
>>> suggested before -- parameterizing attributes:
>>>
>>> final(false) -> remove final
>>>
>>> This allows compile-time booleans to modify attributes in ways that
>>> are extremely difficult in templates today.
>>>
>>
>> I think both ways shoud be available
>>
>> when I need remove just one attr than attr!false is nice but when I
>> need remove for eg: 3 or more it will look like:
>>
>> void someFunc final(false) pure(false) nothrow(false)
>>
>> in this case I think that:
>>
>> default void foo() nothrow looks OK now :)
>>
>
> I'd rather combine the parameterization with attribute sets (i.e. aliasing sets of attributes to one symbol). The 'default' idea is ok, but I think it's too clever -- the word "default" doesn't immediately say "attributes", so it's going to confuse people.
>
> -Steve
It could work both ways at the same time.
Maybe even something like "default(pred) final(pred) nothrow" --> if pred is compile-time-true, reset all attributes and then add final/nothrow; if it's compile-time-false, disable final and enable nothrow.
|
January 10, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Posted in reply to aldanor | On Friday, 9 January 2015 at 14:26:26 UTC, aldanor wrote:
> It could work both ways at the same time.
>
> Maybe even something like "default(pred) final(pred) nothrow" --> if pred is compile-time-true, reset all attributes and then add final/nothrow; if it's compile-time-false, disable final and enable nothrow.
@disable is also currently a keyword.
pure nothrow @safe @nogc immutable
Test
{
//doSomething is nothrow @safe @nogc immutable
@disable(pure) void doSomething() {}
//getAnInt is pure @nogc immutable
@disable(nothrow, @safe) int getAnInt() {}
//typeof(n) == int
@disable(immutable) int n;
}
This is also useful for templated functions, methods, etc. It allows one to tell the compiler that the function in question should never have a particular attribute inferred for it.
//doSomethingElse will never be inferred as pure,
//but may be inferred as nothrow, @safe, or @nogc
@disable(pure) void doSomethingElse()() {}
|
January 10, 2015 Re: Revert attributes to their defaults with default keywords | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta Attachments: | On Sat, 10 Jan 2015 02:47:00 +0000
Meta via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> On Friday, 9 January 2015 at 14:26:26 UTC, aldanor wrote:
> > It could work both ways at the same time.
> >
> > Maybe even something like "default(pred) final(pred) nothrow" --> if pred is compile-time-true, reset all attributes and then add final/nothrow; if it's compile-time-false, disable final and enable nothrow.
>
> @disable is also currently a keyword.
>
> pure nothrow @safe @nogc immutable
> Test
> {
> //doSomething is nothrow @safe @nogc immutable
> @disable(pure) void doSomething() {}
>
> //getAnInt is pure @nogc immutable
> @disable(nothrow, @safe) int getAnInt() {}
>
> //typeof(n) == int
> @disable(immutable) int n;
> }
>
> This is also useful for templated functions, methods, etc. It allows one to tell the compiler that the function in question should never have a particular attribute inferred for it.
>
> //doSomethingElse will never be inferred as pure,
> //but may be inferred as nothrow, @safe, or @nogc
> @disable(pure) void doSomethingElse()() {}
i chose to go with `@impure`, `@canthrow` and `@gc`. the fact is that adding new "inverted" attributes requres adding new STC flags into the compiler. this is very intrusive change. yed UDAs can be checked just before we set `FUNCFLAG*Inprocess`. this was ALOT easier than hunting down all the places where i need to take care on new STC flags.
yes, this is ugly hack with ugly new set of attributes, but it's way easier to support for me. and as i have no hope of taking something like this into mainline (at least not sooner than another year pass), maintenance costs becomes very important. complex PR will rot soon and rotten PR will never make it into mainline.
|
Copyright © 1999-2021 by the D Language Foundation