system("cls") equivalent in D or something similar? - console

How do I clear the console screen in D? In C/C++ I used system("cls"). Is there something similar to it in D?

You can use system("cls") in D too. You just need to import the std.process module:
import std.process;
int main() {
version(windows) {
system("cls");
} else {
system("clear");
}
return 0;
}
However, I suggest avoiding the system() call (read why). I would rather use Console API on Windows, and Curses API on platforms that support curses (ncurses and pdcurses work on Windows too).

Related

Kotlin: double colon (reflection) operator over

So I was working with VertX Web, trying to make it work with Kotlin.
There's a router and you have to say something like
val vertx = Vertx.vertx()
val server = vertx.createHttpServer()
val router = Router.router(vertx)
server.requestHandler(router::accept)
But it doesn't work. What am I doing wrong? When I use it on Kotlin defined classes, it behaves normally. Is it done on purpose?
Whatever, I had to do it manually like this
server.requestHandler{router.accept(it)}
It is a known bug.
See this issue.
A workaround is to use a Lambda instead. e.g.
class Foo {
fun doWork(work: () -> Unit) {
work()
}
}
class Bar (val text: String) {
fun printText() {
println("${text}")
}
}
val foo: Foo = Foo()
val bar: Bar = Bar("Hello Kotlin!")
foo.doWork(bar::printText) //Fails
foo.doWork({ bar.printText() }) //Is working
Technically it's not a bug. I asked early on if they planned to support method references on instances in version 1, and I was told that they most likely wouldn't.
Method references can only be used from classes and modules, not from instances. Coming from Java 8, this seems like a big deal, but considering the potential conciseness of their lambda syntax, it really isn't.
UPDATE: They plan to add this feature in 1.1

How can I write an XQuery that can test what version of XQuery is being used?

I am writing an XQuery that needs to check what version of XQuery is being run (eXist, Saxon, etc.). Is there a standard function that will return the system properties of an XQuery such as version, vendor etc?
I know there are some system specific calls such as eXist's
system:get-version()
but I am trying to find a standard function that would run on all platforms.
For example in XSLT we have:
Version:
<xsl:value-of select="system-property('xsl:version')" />
<br />
Vendor:
<xsl:value-of select="system-property('xsl:vendor')" />
<br />
Vendor URL:
<xsl:value-of select="system-property('xsl:vendor-url')" />
Are their similar functions for XQuery?
You can use the XQuery 3.0 function function-lookup to check the existence of implementation-specific functions to check for the processors. All XQuery 3.0 supporting processors should be able to process this.
declare function local:exist() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('system:get-version'), 0))))
then true()
else false()
} catch * {
false()
}
};
(: works only on Saxon PE and EE, fails in HE since HE does not support XQuery 3.0. It would be nice if saxon had a version function. :)
declare function local:saxon() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('saxon:parse'), 1))))
then true()
else false()
} catch * {
false()
}
};
declare function local:marklogic() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('xdmp:xquery-version'), 0))))
then true()
else false()
} catch * {
false()
}
};
declare function local:basex() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('prof:time'), 1))))
then true()
else false()
} catch * {
false()
}
};
declare function local:get-processor() as xs:string {
if (local:exist()) then "eXist"
else if (local:saxon()) then "Saxon"
else if (local:marklogic()) then "MarkLogic"
else if (local:basex()) then "BaseX"
else "Unknown"
};
local:get-processor()
Unfortunately, I was not able to make this more elegant using Higher-Order functions, as the implementation-specific functions were executed before the function is actually called. But I am sure this could also be written more elegant.
Tricky in the case of Saxon-HE because Saxon-HE only supports XQuery 1.0 and provides no vendor extensions. It does however provide a mechanism for creating user-defined extension functions, and you could use that to implement an interrogative function of your own design.
Or you could define an external global variable and initialize that from the application that runs the query, assuming the application is under your control.
There doesn't appear to be a function in Saxon, but eXist has system:get-version():
http://exist-db.org/exist/apps/fundocs/view.html?uri=http://exist-db.org/xquery/system&location=java:org.exist.xquery.functions.system.SystemModule
Also, although you're not using it, other SO readers may be interested to know that MarkLogic has xdmp:xquery-version():
http://docs.marklogic.com/7.0/xdmp:xquery-version
There is an inherent problem here that different versions of xquery simply wont complile if run by an incompatible version. You cant generally do a
#if version > 1
new stuff
#else
old stuff
#endif
Some vendor extensions supply version information and you can use an "eval()" type epression to get around this, but in pure XQuery there is no point I can think of that can used to do conditional logic that is compilation time valid on the current version yet use some feature on a different version.

Module to wrap generic Timer

I am trying to write a module to wrap a generic Timer, but I would like to use this module to be able to wrap timers with arbitrary precision, without writing duplicated code.
I wrote the module like that, but it complains that precision_tag is missing a type, but also giving it a type it won't compile.
generic module TimerLoggerP(precision_tag) {
provides {
interface Timer<precision_tag> as Timer;
interface LoggerComponent;
}
uses {
interface Timer<precision_tag> as RTimer;
}
}
Does anyone know how to make it work in the generic way I am trying to achieve?
Just needs the special typedef type.
generic module TimerLoggerP(typedef precision_tag) {
provides {
interface Timer<precision_tag> as Timer;
interface LoggerComponent;
}
uses {
interface Timer<precision_tag> as RTimer;
}
}

Enqueue in Phonon no longer working

After a recent software update to the phonon libraries, I've noticed that a media playing application that I wrote is no longer able to loop tracks. The code in question is below. The first track is set, and once it nears completion it is set to play again.
void Alarm::Start(bool useCustom)
{
if(useCustom)
{
media->setCurrentSource(Phonon::MediaSource(this->_CustPath));
this->_UsingCustomPath=true;
}else{
FileIO::ExtractAudio();
media->setCurrentSource(Phonon::MediaSource(this->_DefaultPath));
this->_UsingCustomPath=false;
}
media->play();
connect(media,SIGNAL(aboutToFinish()),this,SLOT(RepeatAllTheThings()));
this->_isPlaying=true;
}
void Alarm::RepeatAllTheThings()
{
if(this->_UsingCustomPath)
{
media->enqueue(this->_CustPath);
}else{
media->enqueue(this->_DefaultPath);
}
}
After running through the debugger a few times I noticed this message:
"Ignoring source as no aboutToFinish handling is in progress"
A quick google search doesn't tell much about this message. It looks like a check for a private variable (that I dont have access to) has been added (a diff of the file)
Does anyone know if I just discovered a new bug in phonon, or am I some how using the enqueue method incorrectly?
EDIT:
The code above only fails about 1/2 the time. Very confused. Currently running phonon-gstreamer 4.6.3
Solved with this work-around:
media->play();
connect(media,SIGNAL(finished()),this,SLOT(RepeatAllTheThings()));
void Alarm::RepeatAllTheThings()
{
if(this->_UsingCustomPath)
{
media->setCurrentSource(Phonon::MediaSource(this->_CustPath));
}else{
media->setCurrentSource(Phonon::MediaSource(this->_DefaultPath));
}
media->play();
}

How to tell if Flex is run in debug mode?

I want to declaratively turn on and off logger for my Flex application and it seems that my usual way of determining if it's debug mode or not works only sometimes. The code for it:
isDebugSWF = new Error().getStackTrace().search(/:[0-9]+]$/m) > -1;
Do you know a better way for it?
Edit:
I posted answer below.
The static property Capabilities.isDebugger in the flash.system.Capabilities class specifies if the Flash player installed is a debug version or not. It requires Flash Player 9 or AIR 1.0 as the minimum version.
Keep in mind though that debug Flash Player installers are publicly available, and that there is nothing stopping users from installing the debug version of Flash. If have something sensitive that you want to hide, using conditional compilation would be a better option.
The previous approach was based on great article . One of the comments suggested that in Release the stacktrace is null, so I modified it properly:
protected function configureLogger() : void
{
if(!isDebugPlayer()|| !isDebugBuild())
{
// stop logging
Logger.hide = true;
}
else
{
// resume logging
Logger.hide = false;
}
}
private function isDebugPlayer() : Boolean
{
return Capabilities.isDebugger;
}
/**
* Returns true if the swf is built in debug mode
**/
private function isDebugBuild() : Boolean
{
var stackTrace:String = new Error().getStackTrace();
if(stackTrace == null || stackTrace.length == 0)
{
//in Release the stackTrace is null
return false;
}
//The code searches for line numbers of errors in StackTrace result.
//Only StackTrace result of a debug SWF file contains line numbers.
return stackTrace.search(/:[0-9]+]$/m) > -1;
}
This way I can finally configure ThunderBoltAS3 logger depending on current build type.
Looks like you are looking for the same, as answered in another post. Look here: How can you tell programmatically if a Flex App is running in debug mode?
We are using conditional compilation at our project :
Adobe Documentation

Resources