www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - sc.ini and delegates

reply "Eugene Yang" <yes9111 gmail.com> writes:
Hello all,

If I want to set custom imports and library search paths for a 
specific project, how do I specify those paths in the sc.ini 
file?  Setting the DFLAGS environment variable seems to override 
the default settings; is there a way I can append to the default 
DFLAGS variable?

Also, if I try to compile this code

import std.stdio, std.algorithm;

char toInverse(in char input)
{
	if(input == 'a')
		return 'b';
	return 'c';
}

void main(string[] args)
{
	string line = readln();
	auto result = line.map!(toInverse);
	writeln(result);
}

I get an error saying that toInverse is not callable with the 
argument type dchar.  Why is line, which should be 
immutable(char)[] being casted as a dchar array?

Thanks in advance,
Eugene Yang
Feb 24 2014
next sibling parent "TheFlyingFiddle" <kurtyan student.chalmers.se> writes:
On Monday, 24 February 2014 at 20:54:55 UTC, Eugene Yang wrote:
 Hello all,

 If I want to set custom imports and library search paths for a 
 specific project, how do I specify those paths in the sc.ini 
 file?  Setting the DFLAGS environment variable seems to 
 override the default settings; is there a way I can append to 
 the default DFLAGS variable?

 Also, if I try to compile this code

 import std.stdio, std.algorithm;

 char toInverse(in char input)
 {
 	if(input == 'a')
 		return 'b';
 	return 'c';
 }

 void main(string[] args)
 {
 	string line = readln();
 	auto result = line.map!(toInverse);
 	writeln(result);
 }

 I get an error saying that toInverse is not callable with the 
 argument type dchar.  Why is line, which should be 
 immutable(char)[] being casted as a dchar array?

 Thanks in advance,
 Eugene Yang
The array itself is not casted to a dchar array, but when iterating over a string using range interfaces (eg map!(...) in your code) it will do the iteration over dchars instead of char. This is to make it simpler to write correct unicode processing code. How this is done is simply that the string.front property has the type dchar and whenever you access it some unicode processing will be done. This is quite counter intuitive the first time you run into it. See http://forum.dlang.org/thread/urhhmrnzovxnyufcipwt forum.dlang.org#post-mailman.384.1389668512.15871.digitalmars-d-l arn:40puremagic.com for a more indepth view on why and how string iteration is implemented in this way.
Feb 24 2014
prev sibling parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On 2/24/2014 3:54 PM, Eugene Yang wrote:
 Hello all,

 If I want to set custom imports and library search paths for a specific
 project, how do I specify those paths in the sc.ini file?  Setting the
 DFLAGS environment variable seems to override the default settings; is
 there a way I can append to the default DFLAGS variable?
sc.ini isn't really the right place for custom or project-specific imports. Just use the compiler's -I... flag, and wrap it up in a one-liner shell script if you need to.
 I get an error saying that toInverse is not callable with the argument
 type dchar.  Why is line, which should be immutable(char)[] being casted
 as a dchar array?
Because of Unicode. Despite the outdated name, a "char" is not really a character, it's a UTF-8 code unit, not a code point, and not a grapheme (ie a full character). See this if you're not already up-to-speed on code units vs code points: http://www.joelonsoftware.com/articles/Unicode.html Working with code units instead of code points is usually the wrong thing to do and leads to lots of bugs with non-english text. So by default, D iterates strings by 32-bit code point, not by code unit (behind the scenes, it calls decode()). Since code points and code units are ONLY equivalent in UTF-32, D always uses the 32-bit dchar to represent code points. So no matter whether you're iterating a UTF-8 string, a UTF-16 wstring or UTF-32 dstring, D's default is to always iterate by decoding one code point (as a dchar) at a time. In the rare cases you want to iterate by individual code units (never do this unless you have a strong grasp of Unicode and really know what you're doing), you can do this: string foo = ...; foreach(char ch; foo) {...}
Feb 24 2014