How to determine if two instances have the same type, in Dart? - class

Let's say I get two instances in my code and I don't know their types. How to check it?
If in Java, I can use this code:
a.getClass() == b.getClass()
But in Dart, I can't find similar methods. Although there is the dart:mirrors providing reflect(instance) function, which may let me do it, but I'm not sure if that's a correct solution since it looks complicated.

a.runtimeType == b.runtimeType

I think dart:mirrors (reflection) API helps you. Look at this page :
http://blog.dartwatch.com/2012/06/dartmirrors-reflection-api-is-on-way.html
Also you can look this question(with runtime solution)
How do I get the qualified name from a Type instance, in Dart?

if you want to compare a and b you can use
if(a.runtimeType == b.runtimeType);
but if you want to confirm that a is the type you want you need to do this
if(a.runtimeType.toString()=="DivElement");//a is a div for instance
because runtimeType's value is not a string

Related

Apache AGE - Creating Functions With Multiple Parameters

I was looking inside the create_vlabel function and noted that to get the graph_name and label_name it is used graph_name = PG_GETARG_NAME(0) and label_name = PG_GETARG_NAME(1). Since these two variables are also passed as parameters, I was thinking that, if I wanted to add one more parameter to this function, then I would need to use PG_GETARG_NAME(2) to get this parameter and use it in the function's logic. Is my assumption correct or do I need to do more tweaks to do this?
You are correct, but you also need to change the function signature in the "age--1.2.0.sql" file, updating the arguments:
CREATE FUNCTION ag_catalog.create_vlabel(graph_name name, label_name name, type new_argument)
RETURNS void
LANGUAGE c
AS 'MODULE_PATHNAME';
Note that all arguments come as a "Datum" struct, and PG_GETARG_NAME automatically converts it to a "Name" struct. If you need an argument as int32, for example, you should use PG_GETARG_INT32(index_of_the_argument), for strings, PG_GETARG_CSTRING(n), and so on.
Yes, your assumption is correct. If you want to add an additional parameter to the create_vlabel function in PostgreSQL, you can retrieve the value of the third argument using PG_GETARG_NAME(2). Keep in mind that you may need to make additional modifications to the function's logic to handle the new parameter correctly.
The answers given by Fahad Zaheer and Marco Souza are correct, but you can also create a Variadic function, with which you could have n number of arguments but one drawback is that you would have to check the type yourself. You can find more information here. You can also check many Apache Age functions made this way e.g agtype_to_int2.

Why bother casting the return value since the type has been specified when calling the function?

I am learning Editor Script of Unity recently, and I come across a piece of code in Unity Manual like this:
EditorWindowTest window = (EditorWindowTest)EditorWindow.GetWindow(typeof(EditorWindowTest), true, "My Empty Window");
I don't know why bother casting the result with (EditorWindowTest) again since the type has been specified in the parameter field of GetWindow().
Thanks in advance :)
There are multiple overloads of the EditorWindow.GetWindow method.
The one used in your code snippet is one of the non-generic ones. It accepts a Type argument which it can use at runtime to create the right type of window. However, since it doesn't use generics, it's not possible to know the type of the window at compile time, so the method just returns an EditorWindow, as that's the best it can do.
You can hover over a method in your IDE to see the return type of any method for yourself.
When using one of the generic overloads of the GetWindow method, you don't need to do any manual casting, since the method already knows at compile time the exact type of the window and returns an instance of that type directly.
The generic variants should be used when possible, because it makes the code safer by removing the need for casting at runtime, which could cause exceptions.
If you closely look, GetWindow's return type is EditorWindow. Not the EditorWindowTest, so typecasting makes sense.
https://docs.unity3d.com/ScriptReference/EditorWindow.GetWindow.html

MapEntry - specify type annotation

I'm using a linter package to practice strict coding, however, I can't figure out how to solve this warning. The code is working but I just want to understand this warning and how to solve this. I'm new to flutter by the way. Hope someone can answer my question. TIA!
Actual Code
PS: sorry, unable to display image because of restriction
The lint you're getting is from pedantic's always_specify_types property. It means that you have to specify the type of the variable. In your case, you need to specify the type of your MapEntry. By default, MapEntry's key and value's type will be dynamic. You'd have to specify it as MapEntry<int,Tab>.
In simple terms, instead of:
var number = 12; // dynamic means that the type can be anything (Eg: String, int, etc.)
do:
int number = 12;

Can anyone explain Swift Combine's Subject.eraseToAnySubject() method and where it should be used?

I can see that Subject.eraseToAnySubject() returns the concrete Subject type AnySubject. I'm assuming this is using a type eraser pattern.
However, the apple docs provide almost no details: https://developer.apple.com/documentation/combine/passthroughsubject/3241547-erasetoanysubject
Can anyone explain how this works and where it should be used?
Also, would it be possible to use the some keyword to avoid using AnySubject?
In Combine, as you chain Publishers to Operators, the return type becomes complicated very quickly since it includes specific detail about each publisher in the chain.
For example a simple string Publisher with a filter and map Operator attached will have a return type of: <Filter<Map<Published<String, Error>>>>
eraseToAny uses a type eraser pattern to capture what's actually important about the return type. In the example given, adding an eraseToAnyPublisher will shorten the type to a more succinct <AnyPublisher<String, Error>>

how different between $_GET and $request->getParameter()

I'm currently studing symfony framework.
and I could not found the answer of how different between $_GET and $request->getParameter().
I can understand the $request->getPrameter() can be used for,
if(isset($_GET['test'])){
$test = $_GET['test'];
}else{
$test = 'Unknown';
}
to
$request->getParameter('test','Unknown');
and anything else? I was expect it filter XSS but I think it doesn't.
For me, $_GET way is much easier, but I feel like I should use the $request->getParameter()
So, I'd like to know exactly how diffrence.
Thanks! :)
Use:
$request['parameter']
This is equivalent to $request->getParameter('parameter', null).
Note that $request->getParameter differs from $_GET in that it returns all parameter types. $request->getGetParameter is equivalent to $_GET.
If you access a request parameter like:
$request->getParameter('parameter');
it can be the value of $_GET['parameter'] or $_POST['parameter'] as well. It is useful as normally you don't care whether the value is coming through post or get method.
You should infact be using $request->getGetParameter('parameter') if you're specifically after a get parameters.
$request->getGetParameter('parameter') is the equivalent of $_REQUEST['parameter'] which may not result in the desired behaviour.
Also worth noting that the sfWebRequest object is available in your views via $sf_request i.e. $sf_request->getGetParameter('parameter')