www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Overriding interface method without implementation

reply "Uranuz" <neuranuz gmail.com> writes:
Sometimes when I build my code I get error message(s) starting
with `undefined reference to`. Because it happens not too often
it's not obvious and easy to understand what is the problem.
There is an easy example:

import std.stdio;

interface I
{
	void foo(int num);
	int bar();	
}

class A: I
{
	override {
		void foo(int num); //This string is evil root))
	
		int bar()
		{	return 100; }
	}
}

void main()
{
	writeln("Hello, world!!!");
}

Compilation output:
/d621/f189.o:(.rodata+0x138): undefined reference to
`_D4f1891A3fooMFiZv'
/d621/f189.o: In function `_TMP3':
/d621/f189.d:(.text._D4f1891A3barMFZi+0x55): undefined reference
to `_D4f1891A3fooMFiZv'
collect2: error: ld returned 1 exit status
--- errorlevel 1

It happens because after invention of some interface I simply
copy-paste its contents into class A that implements I. And
sometimes I can forget to implement some small function that for
example just returns value of some field.

In such cases I will get some not very obvious error that can't
help me to find out module name and line index that produces this
error.

As far as I know this syntax for function without implementation
mean that it will be implemented somewhere else for example in C
code. But I think that for "usual" programmer some understandable
error message or warning is needed.
Nov 17 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
See also:
http://d.puremagic.com/issues/show_bug.cgi?id=5129

The special use case should be respected and allowed, but it 
can't make error messages too much harder for every one else in 
most other common cases.

Bye,
bearophile
Nov 17 2013
parent reply "Uranuz" <neuranuz gmail.com> writes:
As far as I understand it's not a problem at compile stage but 
it's a link error. So compiler can't decide if it's error or not 
when you have function without body. But may be some keyword or 
attribute is needed to tell the compiler if this function will be 
implemented in some other way or it's just a missing body.
Nov 17 2013
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11/17/2013 07:58 PM, Uranuz wrote:
 But may be some keyword or attribute is needed to tell the compiler if
 this function will be implemented in some other way or it's just a
 missing body.
Mark foo 'abstract' and your code will compile.
Nov 17 2013