Why does this getter inside a function produce a syntax error? - dart

In Dart Editor build 27025, the following code produces a syntax error on the line declaring the getter get x:
main() {
var _x;
set x(x) => _x = x; // This is okay
get x => _x; // Syntax error here
}
Note that the setter set x doesn't produce an error. Is this a bug in Dart Editor or am I doing something wrong here?

As you said, getters are functions that are used to retrieve the values of object properties and setters are functions that are used to set the values of object properties. In you example code, _x is not an object property.
The spec shows that getterSignature and setterSignature are only allowed in classes and at the top-level of libraries.
The only thing that surprises me is that your set doesn't produce a syntax error.

That's an interesting one. I think the bug may be that it doesn't complain about the setter. If you delete the getter and just use the setter it doesn't actually run. Or even parse. Which I think means they're not allowed within a function, though it's not immediately clear to me why.

A Dart project member responded to my bug report that "it isn't valid to declare a getter or a setter inside another method or function. This can be seen by following the grammar for a statement to a localFunctionDeclaration to a functionSignature.
That said, it's a bug that there was no syntax error for the setter."

Related

Dart: dynamic vs nullable Object

Is there any difference between dynamic and Object? in dart?
This question may sound a duplicate of this What is the difference between dynamic and Object in dart?. However, I think it is not. I am not talking about semantic difference or any warnings thrown by the dart analyzer, or anything from a coder's perspective. I want to know is there a real difference between both under the hood.
I can assign any data value to both.
When I run the following:
Object? a;
dynamic b;
print(a.runtimeType);
print(b.runtimeType);
I get:
Null
Null
I know dynamic is a keyword, while Object? is a Object class. But does dynamic infer to Object?.
I'm new to Dart lang. So, please enlighten me.
Yes, there is a difference.
The types dynamic and Object? are equivalent wrt. subtyping. Both are "top types" which means that every type is a subtype of them both, even each other.
So, for subtyping there is no difference.
The difference is entirely in what you can do with an expression that has one of those types.
If an expression has type Object?, then the only methods you can call on it are the methods of Object and Null. The only types you can assign the expression to are top types.
If the expression has type dynamic, it is as if the static type system has been turned off.
You are allowed to call any method (like dynamicExpression.arglebargle()) without any warning. If the method isn't there at runtime, it'll throw an error.
And you can assign the value to any type. If the value turns out to not have that type at runtime, it'll throw an error. (This is usually called "implicit downcast" because it works as if an is ExpectedType was added to the expression by the compiler.)
Also, because a dynamic expression is treated as having any method, you cannot call extension methods on it.
It's like dynamic is a type alias for Object? with the extra effect of turning off static type checking.
When you declare a variable as an Object?, during compile-time the compiler knows that type of the variable is Object? and it remains Object? forever. You can assign any type to this variable because every other type either extends the Object or null.
When you declare a variable as a dynamic, during compile-time the compiler does not know the type of the variable and just ignores it. The compiler will check the type only during run-time and will infer the type according to the value you assigned.
dynamic contains Exception. Object can only represent known data types and Null, (excluding Exception)

If a set literal is of type set then what is its class in dart?

So the following code snippet
Set mySet = {1,2,3};
is an instance of type Set which is permissible, however what would the class of the set literal be. I have tried to search for this, however I have found no answer in the dart documentation.
A literal exists only in your source code. Asking for its "class" doesn't make a lot of sense.
Using a Set, Map, or List literal is just syntactic sugar for invoking a corresponding constructor. The Set factory constructor constructs a LinkedHashSet.
However, you'll see that LinkedHashSet is also abstract. Its factory constructor returns an instance of a private, internal class. You can see its typename via print(Set().runtimeType); the actual type might be different for different platforms and is unlikely to be useful to you.

Clang AST - isNullPointerConstant()

I'm trying to check if input arguments to a CallExpr is null/nil in Objective C using Clang's AST
I saw that there is a method isNullPointerConstant() that seems like it will do what I want http://clang.llvm.org/doxygen/classclang_1_1Expr.html#ab56d6fd074c831a3e63b45f4f226b35a
However, I'm not really sure how to invoke this method. Specifically, what is NullPointerConstantValueDependence that it is taking in and what is NullPointerConstantKind of the return?
What I need to do is just evaluate whether an argument is null or not, a boolean return value would work fine. Is there any other way that I should be looking at?
This worked for me in the end
Expr::NullPointerConstantKind kind = expr->isNullPointerConstant(*Context, Expr::NullPointerConstantValueDependence());
If kind is > 0, means there are some form of null pointer and one can retrieve it to see the cause of it.

What is the syntax for implicit cast operator in dart?

I would like to cast instances of my custom class A to int. What is the syntax of the implicit cast operator? (I thought I remembered that there is such a feature but I can't find it on the web)
int a = (new A());
You can also use as to help tell the tools "no, really, treat this object as this type".
A good example of this is when you have to deal with dart:html's querySelector() function.
FormElement form = querySelector('#sign-up') as FormElement;
In the above, the object returned by querySelector('#sign-up') is checked that it is really an instance of FormElement.
Learn more at https://www.dartlang.org/docs/dart-up-and-running/ch02.html#operators
Type annotations are not allowed to affect behavior in Dart. If you're not running in checked mode, then this:
int a = new A();
will work the same as this:
var a = new A();
at run-time. In other words, when not in checked mode, you're welcome to store your A in a variable annotated as an int, but no actual conversion takes place.
If you are running in checked mode, the first form will give you a runtime exception.
I'm not sure, but I think what you're asking for is a way to define a conversion between your class A and int that will happen automatically when "cast" to an int. No such thing exists, to my knowledge. You should simply define a method to do so. For example:
int a = new A().to_i();

Erlang record expression ignored warning

I have following code:
Check#tab_info{login_errors = 0},
{ok, PID};
But i get warning when i try to compile it:
the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)
What's wrong? How can i fix it?
Thank you.
Well, the compiler is telling you exactly what's wrong :) You create a new #tab_info record, but never bind it to any variable. The code is therefore meaningless and the compiler is telling you so. Changing the Check variable (or more correctly, creating a new one) won't have any effect unless you return it. Check is not a global variable, like it might be in imperative languages. Also, changing a variable you receive as an argument to a function, will not result in a change in how the caller sees the variable.
You want to bind the expression to some variable and then do something meaningful with it.
NewCheck = Check#tab_info{...}
{ok, Pid, NewCheck}
As a side note, if instead you did a function call and did not bind the return value to anything, the compiler would not complain as the function might have side-effects and this might be the reason you called the function in the first place.

Resources