www.digitalmars.com         C & C++   DMDScript  

D - Some errors

reply Juarez Rudsatz <juarez correio.com> writes:
D people :

I have found some errors during a simple program coding. 
I dont know if are errors or misunderstanding, but you could evaluate and 
give a final word.
The error are in the nexts post in this thread.

Juarez Rudsatz
Jul 08 2002
next sibling parent reply Juarez Rudsatz <juarez correio.com> writes:
module erro1;

import c.stdio;

void main(){

	// First error : comparing strings with slices
	char[]	s = '1234';

	printf(s[0..3] ~ \n);

	if ('124' == s[0..3]) 
		printf('Test 1');
	else
	if ('123' == s[0..3]) 
		printf('Test 2');
	else
	if ('124' === s[0..3]) 
		printf('Test 3');
	else
	if ('123' === s[0..3]) 
		printf('Test 4');
	else
		printf('Failed');

	// second error : constant string permited in if

	if ('value')
		printf(\n'Test 5'\n);

	// next error : semicollon list permited in if
	if ('value' , s[1..2], 987)
		printf('Test 6'\n);


	// Last error : switch with no case allowed 
	switch(s){
		default:
	}

}
Jul 08 2002
parent "Walter" <walter digitalmars.com> writes:
Thanks for posting these. I need to evaluate them. -Walter

"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns9245878B789B2juarezcom 63.105.9.61...
 module erro1;

 import c.stdio;

 void main(){

 // First error : comparing strings with slices
 char[] s = '1234';

 printf(s[0..3] ~ \n);

 if ('124' == s[0..3])
 printf('Test 1');
 else
 if ('123' == s[0..3])
 printf('Test 2');
 else
 if ('124' === s[0..3])
 printf('Test 3');
 else
 if ('123' === s[0..3])
 printf('Test 4');
 else
 printf('Failed');

 // second error : constant string permited in if

 if ('value')
 printf(\n'Test 5'\n);

 // next error : semicollon list permited in if
 if ('value' , s[1..2], 987)
 printf('Test 6'\n);


 // Last error : switch with no case allowed
 switch(s){
 default:
 }

 }

Jul 11 2002
prev sibling next sibling parent reply Juarez Rudsatz <juarez correio.com> writes:
module error2;

import c.stdio;

void main()
{

// -> D outputs strings in ASCII codepage and not ANSI on windows

	printf("Portuguese special chars : " ~\n);

// D outputs \n not as \n\r in Windows

printf("Execute this program and redirect to a file:\n error > 
output.txt");

}
Jul 08 2002
next sibling parent "Walter" <walter digitalmars.com> writes:
"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns924587ABC5488juarezcom 63.105.9.61...
 module error2;

 import c.stdio;

 void main()
 {

 // -> D outputs strings in ASCII codepage and not ANSI on windows

 printf("Portuguese special chars : " ~\n);

 // D outputs \n not as \n\r in Windows

 printf("Execute this program and redirect to a file:\n error >
 output.txt");

 }

Jul 11 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns924587ABC5488juarezcom 63.105.9.61...
 module error2;

 import c.stdio;

 void main()
 {

 // -> D outputs strings in ASCII codepage and not ANSI on windows

All it does is output to C's stdout.
 printf("Portuguese special chars : " ~\n);

 // D outputs \n not as \n\r in Windows

It should output \r\n.
 printf("Execute this program and redirect to a file:\n error >
 output.txt");

 }

Jul 11 2002
prev sibling next sibling parent Juarez Rudsatz <juarez correio.com> writes:
module error3;

import c.stdio;

void main()
{
// Bug on sorting strings

	char[][] string;
	
	string.length = 2;
	
	string[1] = 'cba';
	
	string[0] = 'zyx';
	
	// This sorts the strings
	string.sort;
	
	// This will crash the compiler
	string[0].sort;
	
	// This will give sintax error
	//string[0].sort();
	
	printf(string[0]);
	printf(string[1]);

}
Jul 08 2002
prev sibling next sibling parent reply Juarez Rudsatz <juarez correio.com> writes:
module error4;

class Xyz
{
	private void SetParam(char[] option)
	{
		// no call for SetParam must be a error ?		
	}
	
}

void main()
{
	
}
Jul 08 2002
parent "Walter" <walter digitalmars.com> writes:
It would be dead code. I suppose it could be considered an error, but it
might be rather annoying because code under development frequently has
functions never called, etc.

"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns924587E2E7E7Ejuarezcom 63.105.9.61...
 module error4;

 class Xyz
 {
 private void SetParam(char[] option)
 {
 // no call for SetParam must be a error ?
 }

 }

 void main()
 {

 }

Jul 19 2002
prev sibling next sibling parent Juarez Rudsatz <juarez correio.com> writes:
module error5;

char[] returnSameString(char[] inputstr)
{
	return inputstr;
}

// This gives a error
char[] passString()
{
	return returnSameString('First string' ~ "Concatenated with second");
}

char[] butThisWorks()
{
	char[] s = 'First string';
	s = s ~ "Concatenated with second";
	return returnSameString(s);
}

main()
{
	
}
Jul 08 2002
prev sibling next sibling parent reply Juarez Rudsatz <juarez correio.com> writes:
module error12345;

// The module stament could be ommited or wrong typed. 
// Is this right ?

void main()
{

}
Jul 08 2002
next sibling parent "Walter" <walter digitalmars.com> writes:
Yes, it's valid code.

"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns9245882B11F8Bjuarezcom 63.105.9.61...
 module error12345;

 // The module stament could be ommited or wrong typed.
 // Is this right ?

 void main()
 {

 }

Jul 24 2002
prev sibling parent reply Pavel Minayev <evilone omen.ru> writes:
On Mon, 8 Jul 2002 16:19:30 +0000 (UTC) Juarez Rudsatz <juarez correio.com> 
wrote:

 // Is this right ?
 
 void main()
 {
 
 }

Just a note: this isn't right, because main() must return int. =)
Jul 24 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374624475681829 news.digitalmars.com...
 On Mon, 8 Jul 2002 16:19:30 +0000 (UTC) Juarez Rudsatz

 wrote:

 // Is this right ?

 void main()
 {

 }

Just a note: this isn't right, because main() must return int. =)

It's the same as with C/C++... Walter, I think you should forbid this, It's lazy coding. Just do this: int main() { return 0; } That is the way it is supposed to be. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Jul 25 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
On Thu, 25 Jul 2002 15:58:32 +0200 "OddesE" <OddesE_XYZ hotmail.com> wrote:

 Just a note: this isn't right, because main() must return int. =)

It's the same as with C/C++... Walter, I think you should forbid this, It's lazy coding. Just do this:

It _is_ forbidden. It's just the compiler doesn't check main() return type. And since it's extern(C), it's all the same to the linker as well... but main() is treated specially anyhow, why not add a check?
Jul 25 2002
next sibling parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374629870745486 news.digitalmars.com...
 On Thu, 25 Jul 2002 15:58:32 +0200 "OddesE" <OddesE_XYZ hotmail.com>

 Just a note: this isn't right, because main() must return int. =)

It's the same as with C/C++... Walter, I think you should forbid this, It's lazy coding. Just do this:

It _is_ forbidden. It's just the compiler doesn't check main() return type. And since it's extern(C), it's all the same to the linker as well...

Ah, I did not know that...
 but main() is treated specially anyhow, why not add a check?

Agreed. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Jul 26 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:CFN374629870745486 news.digitalmars.com...
 It _is_ forbidden. It's just the compiler doesn't check main() return
 type. And since it's extern(C), it's all the same to the linker as
 well... but main() is treated specially anyhow, why not add a check?

Probably a good idea.
Jul 30 2002
prev sibling next sibling parent Juarez Rudsatz <juarez correio.com> writes:
module error7;

/* 	When compiling only this module gives some linking error
	A better message "No main function" for novices ?
    	Is too hard?
*/

class Xyz { }
Jul 08 2002
prev sibling next sibling parent reply Juarez Rudsatz <juarez correio.com> writes:
module error8;

void main()
{

	char[] s;

	s = '';

	// Error message to ugly
	switch(s)
	{
		case 'a' : return;
		case 'b' : return;
	}

}
Jul 08 2002
parent "anderson" <anderson firestar.com.au> writes:
"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns924588673AA44juarezcom 63.105.9.61...
 module error8;

 void main()
 {

 char[] s;

 s = '';

 // Error message to ugly
 switch(s)
 {
 case 'a' : return;
 case 'b' : return;
 }

 }

I know this is not the error your testing for, so shouldn't the switch include a "default:"? Or was that the error you were testing?
Jul 08 2002
prev sibling next sibling parent reply Juarez Rudsatz <juarez correio.com> writes:
module error9;

void main()
{

	char s;
	
	debug(5)
	{
		// Semantical errors are not detected in debug. 
		// Only syntatical are. How I can assure if a 
    	    	// program is debugable?

		s.Value.Anything.Value();
	}

}
Jul 08 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns924588988CEB7juarezcom 63.105.9.61...
 module error9;

 void main()
 {

 char s;

 debug(5)
 {
 // Semantical errors are not detected in debug.

Yes, that is correct.
 // Only syntatical are. How I can assure if a
         // program is debugable?

The only way is to compile with the debug conditionals enabled.
 s.Value.Anything.Value();
 }

 }

Jul 31 2002
parent reply Juarez Rudsatz <juarez nowhere.com> writes:
Why not enforce the code in debug by doing semantical pass?
If you have two conflitant implementations you need use version instead.
This will reduce the amount of code with possible erros.

E.G:

int main()
{

version (x) {
    	char[] s;
}
version (y) {
    	string s;
}

debug()
{
    	version(x) {
    	    	printf(s);
    	}
    	version(y) {
    	    	s.print();
    	}

    	// this will be checked all time for semantic
    	s = getS();
}

}

"Walter" <walter digitalmars.com> wrote in news:ai9que$27he$2
 digitaldaemon.com:

 
 "Juarez Rudsatz" <juarez correio.com> wrote in message
 news:Xns924588988CEB7juarezcom 63.105.9.61...
 module error9;

 void main()
 {

 char s;

 debug(5)
 {
 // Semantical errors are not detected in debug.

Yes, that is correct.
 // Only syntatical are. How I can assure if a
         // program is debugable?

The only way is to compile with the debug conditionals enabled.
 s.Value.Anything.Value();
 }

 }


Aug 01 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Juarez Rudsatz" <juarez nowhere.com> wrote in message
news:Xns925D85FB1293Ajuarezcorreiocom 63.105.9.61...
 Why not enforce the code in debug by doing semantical pass?
 If you have two conflitant implementations you need use version instead.
 This will reduce the amount of code with possible erros.

D is deliberately designed to do a syntax check only (no semantic check) in false debug and version conditionals. The reasons are: 1) faster compilation 2) such code may refer to symbols that do not exist because some debug/version library is missing 3) such code may be works in progress that are not ready yet to be compiled It's intent is to provide an equivalent but superior capability than #ifdef DEBUG / #endif.
Aug 01 2002
next sibling parent reply Juarez Rudsatz <juarez nowhere.com> writes:
"Walter" <walter digitalmars.com> escreveu em
news:aic9so$d33$1 digitaldaemon.com: 

 1) faster compilation
 2) such code may refer to symbols that do not exist because some
 debug/version library is missing
 3) such code may be works in progress that are not ready yet to be
 compiled 
 

Understood ! If the programmer don't deploy the code needed for debug, the mananger could not compile the code. But the problem remains. Only programmer competence will ensure a debugable programm. Then if someone wanna add a little feature or correct a bug, he could have a good surprise. Maybe a third way can be applied.
Aug 01 2002
parent reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
Juarez Rudsatz wrote:
 "Walter" <walter digitalmars.com> escreveu em
 news:aic9so$d33$1 digitaldaemon.com: 
 
 
1) faster compilation
2) such code may refer to symbols that do not exist because some
debug/version library is missing
3) such code may be works in progress that are not ready yet to be
compiled 

Understood ! If the programmer don't deploy the code needed for debug, the mananger could not compile the code. But the problem remains. Only programmer competence will ensure a debugable programm. Then if someone wanna add a little feature or correct a bug, he could have a good surprise. Maybe a third way can be applied.

Maybe we should have a compiler switch that, in addition to building executables for a specific version and debug level, would automatically also do semantic checks on the rest? I can imagine that I would use that often in my makefiles, particularly if I'm doing a debug build anyway. Of course, you could do this with makefiles, if you had a complete list of the potential versions for a given file. To do that, I would like 2 compiler switches. One would examine a set of D files and report all of the versions that can be used on them. The other option would compile things only up to the semantic level. Then I would build a makefile that first gets all of the versions, then test-builds each in turn.
Aug 01 2002
parent reply Juarez Rudsatz <juarez nowhere.com> writes:
Russell Lewis <spamhole-2001-07-16 deming-os.org> wrote in
news:3D49BAF5.5060703 deming-os.org: 

 Maybe we should have a compiler switch that, in addition to building 
 executables for a specific version and debug level, would
 automatically also do semantic checks on the rest?  I can imagine that
 I would use that often in my makefiles, particularly if I'm doing a
 debug build anyway. 
 

After thinking a little bit, I have found this switch : -debug=9 But this dont works when debug is not a integer ( debug(someversion) ).
Aug 05 2002
parent "Walter" <walter digitalmars.com> writes:
"Juarez Rudsatz" <juarez nowhere.com> wrote in message
news:Xns9261BD4C31778juarezcom 63.105.9.61...
 After thinking a little bit, I have found this switch : -debug=9
 But this dont works when debug is not a integer ( debug(someversion) ).

That should be: -debug=someversion
Aug 07 2002
prev sibling parent reply Juarez Rudsatz <juarez nowhere.com> writes:
"Walter" <walter digitalmars.com> wrote in
news:aic9so$d33$1 digitaldaemon.com: 

 1) faster compilation

No doubt! Lets say semantic analisis is 20% of total time, and debug code is 40% of total code, this give us 8% increase.
 2) such code may refer to symbols that do not exist because some
 debug/version library is missing

And what about the version statement into debug statement? debug { someCheck(); version(debugLibraryPresent){ anotherCheck(); } } I know this appear to be redundant. But this solves (2). And the semantic analisis could be enabled without fear.
 3) such code may be works in progress that are not ready yet to be
 compiled 
 

Aug 05 2002
parent "Walter" <walter digitalmars.com> writes:
"Juarez Rudsatz" <juarez nowhere.com> wrote in message
news:Xns9261BEFC44C45juarezcom 63.105.9.61...
 And what about the version statement into debug statement?

 debug {
     someCheck();
     version(debugLibraryPresent){
         anotherCheck();
     }
 }

 I know this appear to be redundant. But this solves (2).
 And the semantic analisis could be enabled without fear.

Yes, that could work.
Aug 07 2002
prev sibling next sibling parent Juarez Rudsatz <juarez correio.com> writes:
module error10;

void main()
{



}

unittest
{
	import string; // Why this is not allowed
}
Jul 08 2002
prev sibling parent reply Juarez Rudsatz <juarez correio.com> writes:
module error11;

void main()
{

	char s;
	
	debug(5)
	{
		import string;	// Why this is not allowed ?
	}

}
Jul 08 2002
parent reply "Walter" <walter digitalmars.com> writes:
Imported symbols into function scope can hide local symbols, making for hard
to find bugs, especially if the symbol is added into string later.

"Juarez Rudsatz" <juarez correio.com> wrote in message
news:Xns924588C5D4E08juarezcom 63.105.9.61...
 module error11;

 void main()
 {

 char s;

 debug(5)
 {
 import string; // Why this is not allowed ?
 }

 }

Aug 01 2002
parent reply Juarez Rudsatz <juarez nowhere.com> writes:
"Walter" <walter digitalmars.com> escreveu em
news:aic9sp$d33$2 digitaldaemon.com: 

 Imported symbols into function scope can hide local symbols, making
 for hard to find bugs, especially if the symbol is added into string
 later. 
 

When I wrote this post I have in mind a simple problem : Attaching library for debuging ( or logging ). If import is not allowed in debug statement I will need another form of doing this. For example, I wanna "printf" out variable values in debug code but in the release I dont need "c.stdio". This is a simple example, and, maybe, could exists anothers more complex. There are another form of doing this ? dmd -JR
Aug 01 2002
next sibling parent "anderson" <anderson firestar.com.au> writes:
1. You could use dynamic loading (using the ones from C), but that's not
ideal.
2. I think D strips all the import code out it doesn't need, so simply write
your own wrapper around printf, and then comment out printf when you don't
needed it. (Does it also strip empty functions?)

"Juarez Rudsatz" <juarez nowhere.com> wrote in message
news:Xns925DC44076579juarezcom 63.105.9.61...
 "Walter" <walter digitalmars.com> escreveu em
 news:aic9sp$d33$2 digitaldaemon.com:

 Imported symbols into function scope can hide local symbols, making
 for hard to find bugs, especially if the symbol is added into string
 later.

When I wrote this post I have in mind a simple problem : Attaching library for debuging ( or logging ). If import is not allowed in debug statement I will need another form of doing this. For example, I wanna "printf" out variable values in debug code but in the release I dont need "c.stdio". This is a simple example, and, maybe, could exists anothers more complex. There are another form of doing this ? dmd -JR

Aug 01 2002
prev sibling parent "Walter" <walter digitalmars.com> writes:
Yes, there's another way. debug works also around global declarations:

debug
{
    import foo;
}

void func()
{
}

"Juarez Rudsatz" <juarez nowhere.com> wrote in message
news:Xns925DC44076579juarezcom 63.105.9.61...
 "Walter" <walter digitalmars.com> escreveu em
 news:aic9sp$d33$2 digitaldaemon.com:

 Imported symbols into function scope can hide local symbols, making
 for hard to find bugs, especially if the symbol is added into string
 later.

When I wrote this post I have in mind a simple problem : Attaching library for debuging ( or logging ). If import is not allowed in debug statement I will need another form of doing this. For example, I wanna "printf" out variable values in debug code but in the release I dont need "c.stdio". This is a simple example, and, maybe, could exists anothers more complex. There are another form of doing this ? dmd -JR

Aug 03 2002