Why Phobos is cool

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Jun 26 05:39:59 UTC 2020


On Fri, Jun 26, 2020 at 03:02:20AM +0000, Jordan Wilson via Digitalmars-d wrote:
[...]
> I don't think I'm a good enough programmer to know that Phobos is bad.
> import std.stdio;
> import std.algorithm;
> import std.range;
> import std.conv : to;
> 
> and I'm a happy man!
[...]

These days, if you use a recent compiler, it's even easier:

	import std;

And off you go. :-P

Only catch is, sometimes you might run into some unfortunate name
conflicts within Phobos (e.g., std.file.remove vs.
std.algorithm.remove).  But for the most part, it's very convenient for
one-off scripts.

Even better yet, I've recently acquired the taste for one-line D
programs using `dmd -i -run -`, and a lot of these take advantage of
`import std;` for its conciseness:

	# Why go through the trouble of creating an entire project just
	# to print Hello World? Just do it in a single line:
	echo 'import std;void main(){writeln("Hello world");}' | dmd -run -

	# What's the value of PI again? Easy:
	echo 'import std;pragma(msg, PI);' | dmd -o- -

	# Test a single line of code to see if it works
	echo 'import std;void main(){writeln([1,2,3].canFind(1));}' | dmd -run -

And also, one-line tests of code in the current directory tree:

	# What's the type of that obscure symbol in module 'mymod'
	# again?
	echo 'import mymod;pragma(msg, typeof(mymod.someSymbol));' | dmd -i -o- -

	# Does function myFun actually work with float arguments??
	echo 'import mymod;pragma(msg, is(typeof(myFun(3.14f))));' | dmd -i -o- -

	# What does function myFun return in this case?
	echo 'import mymod;void main(){writeln(myFun(3.14f));}' | dmd -i -run -

Or generate complex output for other programs using the expressive power
of D code:

	# Generate test data for some external program
	echo 'import std;void main(){writeln(iota(0.0, 100.0).map!(x=>sin(x)).filter!(x=>x > 0.5));}' |  dmd -run - | sort


Maybe we should have a Most Useful One-Line D Program contest. :-P


T

-- 
People say I'm arrogant, and I'm proud of it.


More information about the Digitalmars-d mailing list