Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 27, 2018 Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
For example class A { @recursive @safe void talk() { writeln("Hi"); } } class B : A { override void talk() // @safe attribute added by recursive attribute and can not be removed { writeln("Bye"); } } I have notice that potential bugs can slip by the compiler during compile time, and I purpose this as way to counter them. Alex |
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 12345swordy | On 03/27/2018 10:39 PM, 12345swordy wrote: > class A > { > @recursive @safe void talk() [...] > } > class B : A > { > override void talk() // @safe attribute added by recursive attribute and can not be removed [...] > } It already works like that. B.talk is @safe, and you can't make it @system. You can mark it as @system, that gets overridden by A.talk's @safe. https://run.dlang.io/is/BlH8bp |
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Tuesday, 27 March 2018 at 20:49:25 UTC, ag0aep6g wrote: > On 03/27/2018 10:39 PM, 12345swordy wrote: >> class A >> { >> @recursive @safe void talk() > [...] >> } >> class B : A >> { >> override void talk() // @safe attribute added by recursive attribute and can not be removed > [...] >> } > > It already works like that. B.talk is @safe, and you can't make it @system. You can mark it as @system, that gets overridden by A.talk's @safe. > > https://run.dlang.io/is/BlH8bp Then explain this then. https://run.dlang.io/is/S2KLs5 |
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 12345swordy | On 03/27/2018 11:02 PM, 12345swordy wrote:
> Then explain this then.
> https://run.dlang.io/is/S2KLs5
B.talk is @safe. The compiler ignores the @system attribute on B.talk, because A.talk's @safe attribute takes precedence.
|
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Tuesday, 27 March 2018 at 21:05:32 UTC, ag0aep6g wrote:
> On 03/27/2018 11:02 PM, 12345swordy wrote:
>> Then explain this then.
>> https://run.dlang.io/is/S2KLs5
>
> B.talk is @safe. The compiler ignores the @system attribute on B.talk, because A.talk's @safe attribute takes precedence.
Shouldn't it give a warning then?
|
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 12345swordy | On 03/27/2018 11:10 PM, 12345swordy wrote:
> Shouldn't it give a warning then?
I wouldn't mind a warning, or even an error. Putting both @safe and @system directly on a function is an error, too.
|
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Tuesday, 27 March 2018 at 21:25:33 UTC, ag0aep6g wrote:
> On 03/27/2018 11:10 PM, 12345swordy wrote:
>> Shouldn't it give a warning then?
>
> I wouldn't mind a warning, or even an error. Putting both @safe and @system directly on a function is an error, too.
shouldn't it create a overload?
for example this fails:
class A
{
void talk()@safe {}
}
class B : A
{
alias talk = A.talk;
void talk()@system {}
}
but this works:
class A
{
void talk() {}
}
class B : A
{
alias talk = A.talk;
void talk(int) {}
}
this works also:
class C
{
void talk()@system {}
void talk()@safe {}
}
|
March 28, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to arturg | On 03/28/2018 12:19 AM, arturg wrote: > shouldn't it create a overload? I don't think so. As far as I know, you can't overload on attributes. [...] > but this works: > > class A > { > void talk() {} > } > > class B : A > { > alias talk = A.talk; > void talk(int) {} > } Because different parameters make overloads. > this works also: > > class C > { > void talk()@system {} > void talk()@safe {} > } DMD might accept that, but I don't think it works in a meaningful way. How do you call the @system one? Looks like the @safe one will always be called, even from @system code: ---- import std.stdio; void talk() @system { writeln("@system"); } void talk() @safe { writeln("@safe"); } void main() @system { talk(); /* Prints "@safe". */ } ---- |
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Tuesday, 27 March 2018 at 23:23:38 UTC, ag0aep6g wrote:
>
> DMD might accept that, but I don't think it works in a meaningful way. How do you call the @system one?
>
> Looks like the @safe one will always be called, even from @system code:
>
> ----
> import std.stdio;
>
> void talk() @system { writeln("@system"); }
> void talk() @safe { writeln("@safe"); }
>
> void main() @system
> {
> talk(); /* Prints "@safe". */
> }
> ----
you can call them with __traits(getOverloads, T, "name")[index];
you can overload on types attributes and linkage, but seems like you can only merge overloads based on types.
|
March 27, 2018 Re: Recursive attribute for virtual functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to arturg | On Tuesday, 27 March 2018 at 23:34:20 UTC, arturg wrote:
> On Tuesday, 27 March 2018 at 23:23:38 UTC, ag0aep6g wrote:
>>
>> DMD might accept that, but I don't think it works in a meaningful way. How do you call the @system one?
>>
>> Looks like the @safe one will always be called, even from @system code:
>>
>> ----
>> import std.stdio;
>>
>> void talk() @system { writeln("@system"); }
>> void talk() @safe { writeln("@safe"); }
>>
>> void main() @system
>> {
>> talk(); /* Prints "@safe". */
>> }
>> ----
>
> you can call them with __traits(getOverloads, T, "name")[index];
>
> you can overload on types attributes and linkage, but seems like you can only merge overloads based on types.
i have some templates which can be used like this:
type.dgAt!("name", index);
dgAt!("name", index, somemodule);
dgAt!("name", index, "somemodule");
alias fun = aAt!("name", index, someTypeOrModule);
type.dgOf!("name", void function(int)@safe);
dgOf!("name", void function(int)@safe, module);
dgOf!("name", void function(int)@safe, "module");
from!(type, "name").aAt!1;
from!(type, "name").aOf!(void function(int));
from!(type, "name").dgAt!1;
from!(type, "name").dgOf!(void function(int));
but this fails:
type.dgOf!("name", extern(C) void function(int)@safe);
and this works:
alias funtype = extern(C) void function(int)@safe;
type.dgOf!("name", funtype);
|
Copyright © 1999-2021 by the D Language Foundation