www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Revert attributes to their defaults with default keywords

reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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
Jan 09 2015
next sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
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:
=20
 class A {
      final:
      nothrow:
      ...
      some methods
      ...
 }
=20
 Problem comes when I need add methods which for eg.: throws or=20
 need to be virtual.
=20
 I can put them before final: but this is not perfect, because I=20
 prefer when methods are place in specific order (method abc call=20
 method asd so asd is bellow abc and so on).
=20
 So today I download dmd source and make some small modification=20
 (only few lines) and this is the result:
=20
 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?
Jan 09 2015
prev sibling next sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d writes:
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 { ... }}
Jan 09 2015
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Jan 09 2015
parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d writes:
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 :)
Jan 09 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Jan 09 2015
parent reply "aldanor" <i.s.smirnov gmail.com> writes:
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.
Jan 09 2015
parent reply "Meta" <jared771 gmail.com> writes:
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()() {}
Jan 09 2015
parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
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"=20
 --> if pred is compile-time-true, reset all attributes and then=20
 add final/nothrow; if it's compile-time-false, disable final=20
 and enable nothrow.
=20 disable is also currently a keyword. =20 pure nothrow safe nogc immutable Test { //doSomething is nothrow safe nogc immutable disable(pure) void doSomething() {} =20 //getAnInt is pure nogc immutable disable(nothrow, safe) int getAnInt() {} =20 //typeof(n) =3D=3D int disable(immutable) int n; } =20 This is also useful for templated functions, methods, etc. It=20 allows one to tell the compiler that the function in question=20 should never have a particular attribute inferred for it. =20 //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.
Jan 09 2015
prev sibling parent ketmar via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Fri, 9 Jan 2015 13:16:48 +0100
Daniel Koz=C3=A1k 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=C3=A1no:
=20
 can it be used like this:
=20
   final:
   nothrow:
     ...
     default void foo () { ... }
     ...
=20
 so only `foo` becomes default, but all other methods after `foo` are
 `final nothrow`?
=20 not now, but I plan add this for now you can use: default { void foo () { ... }} =20
=20
 and can it be used like this:
=20
   default void foo () nothrow { ... }
=20
 so `default` resets all attrs and then i can specify another set of
 attrs inline?
=20 same as the first one but this works: =20 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! ;-)
Jan 09 2015