Dart overriding unary minus operator - dart

As per Language specs (10.1.1 Operators) I am trying to override some operators.
I get an analyzer error when overriding the 'minus' and 'unary minus' operators - one that I don't get:
'The operator "-" is not defined on class Indentation'
but in the class I have defined it:
Indentation operator -() {
level--;
return this;
}
and I use it like myInstance--; and it actually does work, but still the analyzer complains and I cannot submit the code 'clean' because of the error.
I have looked up an old thread (Why does overriding negate cause static warning in Dart) but I think it is not relevant here.
Any advise is welcome.

--x is the same as x -= 1. To use it you have to define the operator -(p) (not operator -())
Indentation operator -(n) => new Indentation(level - n);

Related

F# seems to not leverage implicit .NET conversion operators

According to
https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0#actionresultt-type
https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0#asynchronous-action-1
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.actionresult-1?view=aspnetcore-6.0#operators
The following F# code should be legitimate:
[<HttpPost("post-data-2")>]
[<ProducesResponseType(StatusCodes.Status200OK)>]
[<ProducesResponseType(StatusCodes.Status500InternalServerError)>]
member this.PostData2(data: string): Task<ActionResult<int>> =
task {
try
return this.Ok(0)
with | x ->
return this.StatusCode(StatusCodes.Status500InternalServerError, -1)
}
Instead I get two compilation errors in the two 'return' lines
Error FS0193 Type constraint mismatch. The type
'OkObjectResult' is not compatible with type
'ActionResult'
and
Error FS0193 Type constraint mismatch. The type
'ObjectResult' is not compatible with type
'ActionResult'
This works however:
[<HttpPost("post-data-1")>]
[<ProducesResponseType(StatusCodes.Status200OK)>]
[<ProducesResponseType(StatusCodes.Status500InternalServerError)>]
member this.PostData1(data: string): Task<ActionResult<int>> =
task {
try
return ActionResult<int>(this.Ok(0))
with | x ->
return ActionResult<int>(this.StatusCode(StatusCodes.Status500InternalServerError, -1))
}
Why are the implicit cast operators not recognized by F#?
Not sure there can be much of an answer besides "because the language designers decided for it to be that way". Implicit conversions are only used in a narrow set of circumstances in F#.
In addition to calling the constructor as you have, you should also be able to explicitly call ActionResult.op_Implicit or define you own implicit conversion operator.

Does the using declaration allow for incomplete types in all cases?

I'm a bit confused about the implications of the using declaration. The keyword implies that a new type is merely declared. This would allow for incomplete types. However, in some cases it is also a definition, no? Compare the following code:
#include <variant>
#include <iostream>
struct box;
using val = std::variant<std::monostate, box, int, char>;
struct box
{
int a;
long b;
double c;
box(std::initializer_list<val>) {
}
};
int main()
{
std::cout << sizeof(val) << std::endl;
}
In this case I'm defining val to be some instantiation of variant. Is this undefined behaviour? If the using-declaration is in fact a declaration and not a definition, incomplete types such as box would be allowed to instantiate the variant type. However, if it is also a definition, it would be UB no?
For the record, both gcc and clang both create "32" as output.
Since you've not included language-lawyer, I'm attempting a non-lawyer answer.
Why should that be UB?
With a using delcaration, you're just providing a synonym for std::variant<whatever>. That doesn't require an instantiation of the object, nor of the class std::variant, pretty much like a function declaration with a parameter of that class doesn't require it:
void f(val); // just fine
The problem would occur as soon as you give to that function a definition (if val is still incomplete because box is still incomplete):
void f(val) {}
But it's enough just to change val to val& for allowing a definition,
void f(val&) {}
because the compiler doesn't need to know anything else of val than its name.
Furthermore, and here I'm really inventing, "incomplete type" means that some definition is lacking at the point it's needed, so I expect you should discover such an issue at compile/link time, and not by being hit by UB. As in, how can the compiler and linker even finish their job succesfully if a definition to do something wasn't found?

Does Dart have a comma operator?

Consider the following line of code that doesn't compile in Dart -- lack of comma operator, but comparable things are totally fine in JavaScript or C++:
final foo = (ArgumentError.checkNotNull(value), value) * 2;
The closest I could get with an ugly workaround is
final foo = last(ArgumentError.checkNotNull(value), value) * 2;
with function
T last<T>(void op, T ret) => ret;
Is there a better solution?
Dart does not have a comma operator similar to the one in JavaScript.
There is no obviously better solution than what you already have.
The work-around operation you introduced is how I would solve it. I usually call it seq for "sequence" if I write it.
There is sadly no good way to use an extension operator because you need to be generic on the second operand and operators cannot be generic. You could use an extension method like:
extension Seq on void {
T seq<T>(T next) => next;
}
Then you can write ArgumentError.checkNotNull(value).seq(value).
(For what it's worth, the ArgumentError.checkNotNull function has been changed to return its value, but that change was made after releasing Dart 2.7, so it will only be available in the next release after that).
If the overhead doesn't matter, you can use closures without arguments for a similar effect (and also more complex operations than just a sequence of expressions).
final foo = () {
ArgumentError.checkNotNull(value);
return value;
} ();
This is not great for hot paths due to the overhead incurred by creating and calling a closure, but can work reasonably well outside those.
If you need this kind of test-plus-initialization pattern more than once, the cleanest way would arguably be to put it in a function of its own, anyway.
T ensureNotNull<T>(T value) {
ArgumentError.checkNotNull(value);
return value;
}
final foo = ensureNotNull(value);

Extending XFeatureCall scope

This has been puzzling me for a while... I have done research, tried lots of things but failed miserably. The time has come to ask here.
My grammar has this rule to define types:
MyTypeDeclaration returns XExpression:
=>({MyTypeDeclaration} type=JvmTypeReference name=ValidID '(')
(params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)?
')' block=XBlockExpression
;
Of course, in the inferrer, I map it to a class (with a supertype MySupertype to differentiate from other Java classes)
members += f.toClass(f.fullyQualifiedName) [
superTypes += typeRef(MySupertype)
...
members += f.toConstructor [
for (p : f.params)
parameters += p.toParameter(p.name, p.parameterType)
body = f.block
]
...
]
What I need is to invoke this class as a function, e.g. using XFeatureCall. XFeatureCall::feature is a JvmIdentifiableElement and so is MyTypeDeclaration when mapped (in the compiler I will add a "new" prefix to call the class constructor). However, naturally, XFeatureClass does not include Java classes in its default scope.
So the question is, how to change this behavior? I need to include MyTypeDeclaration instances (or, more generally, Java classes with MySupertype as superclass) in XFeatureClass scope. I looked at the type computer, getLinkingCandidates and al but it looks too arcane for me.
I use version 2.15 as I need GWT...
Please help as I am really stuck at this point...
Thanks,
Martin

Very untyped and recursive class definition that compiles but seems a corner case to me

These 4 lines compile but do not make sense to me :
open System
type mclas (y) =
member x.m = x.m
let z = mclas (1:>obj)
Question : In what case would we need to code in such way ? Am I activating subtle class features I am not aware of ?
Edit : If there is no use case, what is the status of this piece of code regarding compiler warning/error and is it eligible for some Issue raising on github ?
Note : At runtime, the debugger cannot evaluate variable z saying "Function evaluation timed out".
The member m actually compiles to something like this in IL:
.property instance object m {
.get instance object Program/mclas::get_m()
}
So m is a property of type object which has a getter that recurses endless. The C# equivalent of this would be:
public class mclas
{
public mclas(object y) { }
public object x {
get {
return x;
}
}
}
Because the recursion never ends, taking too long and/or there is a StackOverflowException happening when the Debugger tries to evaluate m, it cancels and spits out that the evaluation timed out.
And for your actual question: I don't think that you ever need this kind of self-reference in F#, at least I can't think of any possible use.
I think that this behaviour of the compiler makes sense, because a member without paramters will always compile to a get-only property, and then this would be the most obvious way of defining an infinite recursing property (just because this has no use doesn't mean that you can't do it).

Resources