How does Langage.Haskell.TH.report work? - haskell

Unfortunately, many Template Haskell functions have absolutely no documentation at all. One such function is report. It takes a Bool and a String, and produces a compilation error with the specified string as the error message. Does anybody have any clue what the hell the Bool is for? As best as I can tell, either value does exactly the same thing...

If the Bool is True, an error is reported; if it is False, a "warning" is reported, meaning that the template code will continue to run to collect more "warnings."

Looking at the source code, report calls qReport, which is a method of some class called Quasi. This method actually has some damned documentation - though only a tiny snippet. I quote:
Report an error (True) or warning (False) ...but carry on; use fail to stop
So it seems to make my TH splice crash with an appropriate error message, I just need to call fail instead. Hopefully this information will be useful to anyone else trying to figure that out...

Related

How to check if ID3D12GraphicsCommandList has been closed?

I'm learning DirectX12 and writing some utility classes to encapsulate functionality. Right now I'm working on mechanism for pooling CommandLists.
The pool assumes all command lists are closed. I wanted to validate that during inserting to the pool, but I can't manage to check it. From MSDN:
Returns S_OK if successful; otherwise, returns one of the following
values:
E_FAIL if the command list has already been closed, or an invalid API was called during command list recording.
Which is precisely what I'm looking for, but when I call ID3D12GraphicsCommandList::Close() to validate, it throws exception in KernelBase.dll. It looks really bizarre to me. Is this specification incompliance?
//EDIT: I cannot catch the exception, even with catch(...). It tells me maybe something may be wrong with my setup, but everything else is working for me.

antlr4 error handling doesn't even compile

I followed the instructions for error handling at the antlr website http://www.antlr2.org/doc/err.html (it says antlr2 but I couldn't find an alternative for antlr4) and wrote the exception handling for my rule as below.
subStmt :
(visibility WS)? (STATIC WS)? SUB WS? ambiguousIdentifier (WS? argList)? endOfStatement
block?
END_SUB
;
exception
catch [RecognitionException ex] {
}
But when I try to generate the Parser for the grammar it fails as below:
java -jar ../Downloads/antlr-4.7-complete.jar vba.g4 -package vba -o out
error(50): vba.g4:500:4: syntax error: 'catch' came as a complete surprise to me while matching rule preamble
Any help is much appreciated.
Antlr4 is quite different to Antlr2. Start by having a look at this question (and the answer):
How to implement error handling in ANTLR4
Update with a simple approach:
For error reporting, the basic approach is to make a class that implements the ANTLRErrorListener interface. BaseErrorListener has empty implementations for all the methods so you only need to implement the ones you care about. You probably care about syntaxError() the most.
On your parser object, call removeErrorListeners() to clear the internal error listener, and then call addErrorListener() with an instance of the class you want to handle your errors.
You will then get syntaxError() calls on that class when errors are encountered during a parse.
The other methods might (or might not) let you do what you want to do; I haven't used this interface to recover from parse errors.
For recovering from particular errors, you can implement a class with the ANTLRErrorStrategy interface. That gets complicated; see DefaultErrorStrategy for the default implementation.
A very simple approach is to deal with the possible errors in your grammar. Not sure how far you can go with this, but this is likely to be the easiest approach.

Common php functions in hack

I decided to start a new project to get into hacklang, and after fixing some if the problems I initially ran into transitioning from php habits, I ran into the following errors:
Unbound name: str_replace
Unbound name: empty
Doing some research I found that this is due to using 'legacy' php which isn't typechecked, and will error with //strict.
That's fine and all, empty() was easy enough to replace, however str_replace() is a bit more difficult.
Is there an equivalent function that will work with //strict? Or at least something similar.
I'm aware that I could use //decl but I feel like that defeats the purpose in my case.
Is there at least any way to tell which functions are implemented in hack and which are not in the documentation as I couldn't find one?
For reference (though it isn't too relevant to the question itself), here is the code:
<?hh //strict
class HackMarkdown {
public function parse(string $content) : string {
if($content===null){
throw new RuntimeException('Empty Content');
}
$prepared = $this->prepare($content);
}
private function prepare(string $contentpre) : Vector<string>{
$contentpre = str_replace(array("\r\n","\r"),"\n",$contentpre);
//probably need more in here
$prepared = Vector::fromArray(explode($contentpre,"\n"));
//and here
return $prepared;
}
}
You don't need to change your code at all. You just need to tell the Hack tools about all the inbuilt PHP functions.
The easiest way to do this is to download this folder and put it somewhere in your project. I put it in a hhi folder in the base of my project. The files in there tell Hack about all the inbuilt PHP functions.
Most of them don't have type hints, which can lead to Hack thinking the return type of everything is mixed instead of the actual return, that is actually correct in most cases as, for example, str_replace can return either a string or a bool. However, it does stop the "unbound name" errors, which is the main reason for adding them.

The specified object is not recognized as a fake object. Issue

I am having an issue where a FakeItEasy call in an extremely simple test is failing with the error "The specified object is not recognized as a fake object." The call is simple:
A.CallTo(myService.MyMethod(listOfStringsFilter)).MustHaveHappened();
The fake is similarly simple (A.Fake()), and fakes out an interfance with one method, that takes in a list and returns a list. In debug mode, I see the instance of myService is of type {Fake IMyInterface}. Anyway, this issue is really holding me up, thanks in advance for your help.
Update:
This was my own darn mistake, I needed to make the call say:
A.CallTo(() => myService.MyMethod(listOfStringsFilter)).MustHaveHappened();
This was my own darn mistake, I needed to make the call say:
A.CallTo(() => myService.MyMethod(listOfStringsFilter)).MustHaveHappened();

compiler warning on (ambiguous) method resolution with named parameters

One question regarding whether the following code should yield a compiler warning or not (it doesn't). It declares two methods of the same name/return type, one has an additional named/optional parameter with default value.
NOTE: technically the resolution isn't ambiguous, because the rules clearly state that the first method will get called. See here, Overload resolution, third bullet point. This behavior is also intuitive to me, no question.
public void Foo(int arg) { ... }
public void Foo(int arg, bool bar = true) { ...}
Foo(42); // shouldn't this give a compiler warning?
I think a compiler warning would be kind of intuitive here. Though the code technically is clean (whether it is a sound design is a different question:)).
I disagree that it needs a warning, actually. The main issue is, that code is potentially legitimate, and if that's the case you would have to explicitly disable the warning.
What I mean is, in general when you get a warning, you will be able to change your code to get rid of the warning (and presumably make the code better at the same time). But in this case, it may be that you did it like that intentionally and would not be able to change your code to get rid of the warning.
For example, the "unreachable code" warning is something you can just delete the unreachable code to get rid of the warning. Or the "could not find reference" warning - this is usually a signal that you're going to get "undefined type" errors, but if not then you can simply delete the reference. Or maybe "A previous catch clause already catches all exceptions" warning: in this case, you need to change your code so that either the new clause comes before the catch-all, or remove the catch altogether.
But the point is that in every case when you get a warning, you should change your code, and making the change will always result in "better" code. However, in the case of this question, the call is not ambiguous (as far as the compiler is concerned) and I don't think you can argue that it's always a mistake to write code like that, so therefore there shouldn't be a warning.
If the compiler issued a warning about every case where you do something that's probably not the best idea, then we'd be inundated with warnings!
Warnings are to notify programmers of potentially dumb mistakes. This is an area that could generate a dumb mistake, so yes, it should generate a warning. Are you trying to form a petition?

Resources