www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - shared interfaces

reply "Andrey Derzhavin" <vangelisforever yandex.ru> writes:
interface IA
{
	void fnA();
}


interface IB
{
	void fnB();
}


shared interface IC : IA, IB
{
	void fnC();
}



shared class C : IA, IB
{

	override void fnA() // Error: function main.C.fnA does not 
override any function, did you mean to override 'main.IA.fnA'?
	{

	}

	override void fnB() // Error: function main.C.fnB does not 
override any function, did you mean to override 'main.IA.fnA'?
	{

	}


shared class D : IC
{
	
	override void fnC()
	{
	}
	
	override void fnA() // Error: function main.D.fnA does not 
override any function, did you mean to override 'main.IA.fnA'?
	{
		
	}
	
	override void fnB() // Error: function main.D.fnB does not 
override any function, did you mean to override 'main.IB.fnB'?
	{
		
	}
}

what is wrong in declarations, if I need to declare shared 
classes D and C?
Feb 15 2015
parent reply "anonymous" <anonymous example.com> writes:
On Sunday, 15 February 2015 at 10:43:46 UTC, Andrey Derzhavin 
wrote:
 what is wrong in declarations, if I need to declare shared 
 classes D and C?
`shared` on a class/interface makes all members shared. And that's all it does. So this: ---- interface IA { void fnA(); } shared class C : IA { override void fnA() {} } ---- is the same as this: ---- interface IA { void fnA(); } class C : IA { override void fnA() shared {} } ---- But you can't override a non-shared method with a shared one, hence the error. So, if you need to override non-shared methods, you can't mark the whole class shared. Instead, you have to mark the shared members individually. Like so: ---- interface IA { void fnA(); } shared interface IC : IA { void fnC(); } class D : IC { override void fnC() shared {} override void fnA() {} } ----
Feb 15 2015
parent reply "Andrey Derzhavin" <vangelisforever yandex.ru> writes:
On Sunday, 15 February 2015 at 11:30:46 UTC, anonymous wrote:
 On Sunday, 15 February 2015 at 10:43:46 UTC, Andrey Derzhavin 
 wrote:
 what is wrong in declarations, if I need to declare shared 
 classes D and C?
`shared` on a class/interface makes all members shared. And that's all it does. So this: ---- interface IA { void fnA(); } shared class C : IA { override void fnA() {} } ---- is the same as this: ---- interface IA { void fnA(); } class C : IA { override void fnA() shared {} } ---- But you can't override a non-shared method with a shared one, hence the error. So, if you need to override non-shared methods, you can't mark the whole class shared. Instead, you have to mark the shared members individually. Like so: ---- interface IA { void fnA(); } shared interface IC : IA { void fnC(); } class D : IC { override void fnC() shared {} override void fnA() {} } ----
Try to compile your example, it is wrong.
Feb 15 2015
parent reply "anonymous" <anonymous example.com> writes:
On Sunday, 15 February 2015 at 12:34:50 UTC, Andrey Derzhavin 
wrote:
 On Sunday, 15 February 2015 at 11:30:46 UTC, anonymous wrote:
[...]
 ----
 interface IA
 {
    void fnA();
 }
 shared interface IC : IA
 {
    void fnC();
 }
 class D : IC
 {
    override void fnC() shared {}
    override void fnA() {}
 }
 ----
Try to compile your example, it is wrong.
Hm? Works for me. What error do you get, or what doesn't work? Which compiler are you using, which version?
Feb 15 2015
next sibling parent "Andrey Derzhavin" <vangelisforever yandex.ru> writes:
On Sunday, 15 February 2015 at 14:59:20 UTC, anonymous wrote:

 Hm? Works for me. What error do you get, or what doesn't work? 
 Which compiler are you using, which version?
The error "Error: function main.C.fnA does not override any function, did you mean to override 'main.IA.fnA'?" has occured (( I'm using DMD 2.066.1 compiler.
Feb 15 2015
prev sibling parent "Andrey Derzhavin" <vangelisforever yandex.ru> writes:
On Sunday, 15 February 2015 at 14:59:20 UTC, anonymous wrote:

Sorry everything is OK. It's my fault. Thanks))
Feb 15 2015