Working on a patch for some issues in the Dart Analyzer, I need to understand whether Null itself is considered a nullable type.
Currently, the least upper bound computed by Dart’s type algebra for String and Null is String.
However, in my opinion, the type union of String and Null is a nullable String.
Ticket with patch: https://github.com/dart-lang/sdk/issues/38585 Note that the patch still has a bug in widening T to T? if a closure returns both T and null.
Bonus question (ticket https://github.com/dart-lang/sdk/issues/38623): Is Null itself a nullable type and should be suffixed with ‘?’?
My opinion as proof by contradiction (LUB is least upper bound function):
Assume that Null is not nullable.
That means Null and Null? are different types.
LUB(T, Null) = T?, i.e. combining T with Null widens T to T?
Hence, LUB(Null, Null) = Null?
That violates LUB being reflexive which would mean that LUN(Null, Null) = Null
Hence the assumption is wrong.
Dart does not, yet, have non-nullable types. So, the type written as String is a nullable string. The Null type is considered a subtype of any type, and therefore the least upper bound of Null and (nullable) String is (nullable) String.
When Dart gets non-nullable types, that will change. Then the type written String will be non-nullable, and String? will be nullable, and the least upper bound of Null and String should then (hopefully!) be String?. Non-nullable types are not yet available, but they are being designed and are expected to be released ... well, when they are ready.
If you are trying to patch the Dart Analyzer, then you need to be aware of both type systems at the same tiem, because the analyzer has already been modified to recognize some non-nullable types if you pass the proper flags and proper source code.
Related
I want to know the default values of data-types in Dart.
Integer,Double,String,Dynamic and Boolean.
The default value for all types in Dart are null if nothing else is specified when declaring the variable. This includes int, double, String and so on.
Uninitialized variables that have a nullable type have an initial value of null. (If you haven’t opted into null safety, then every variable has a nullable type.) Even variables with numeric types are initially null, because numbers—like everything else in Dart—are objects.
https://dart.dev/guides/language/language-tour#default-value
Is there a noticeable difference between the two?
var example = ["some","content",11,45,true];
List example = ["some","content",11,45,true];
With var example the type (static and runtime) for example will be inferred from the assigned value ["some","content",11,45,true] which will be List (or actually List<dynamic>)
With List example the type will not be inferred but the explicitely provided type List (or actually List<dynamic> if no generic type is provided) will be used instead.
For var example = ["some","content","11","45","true"]; the inferred type would be List<String>.
As far as I know and as simple as I can be;
List is a data type just like some other built-in types in Dart such as String, int and bool. When you initialize a variable using List, the assigned value must be of List type. i.e. You cannot do this
List example = "sometext";
because you're trying to assign a String value to a List variable or object here.
Whereas, var is a way to declare a variable without specifying its type. For var will accept all kind of data types.
Is there a noticeable difference between the two?
var example = ["some","content",11,45,true];
List example = ["some","content",11,45,true];
Both the methods of declaration have same effect unless you expect to assign a value to example with type(s) other than List during it's lifetime. i.e If you're looking to assign an int or double or string or whatever value to example in future use the first method else you can use any one of them.
In the examples I've checked, the operand is always a primitive value, like:
http://host/service/Products?$filter=MainIngredient eq 'Milk'
what if the MainIngredient property is an entity, and I want to reference exactly this entity? Abstracting other cases, that would be something like
http://host/service/Products?$filter=MainIngredient eq Ingredient('770d5720-9ae8-11e3-a5e2-0800200c9a66)
...or isn't the filter not the correct instrument to use, at all?
I think you mean that MainIngredient is the name of a navigation property that points to a single entity of type Ingredient, such as:
http://host/service/Products(1234)/MainIngredient
If this is the case, and assuming that the key of Ingredient is called "ID" then I think the correct form of URL for what you want to do is:
http://host/service/Products?$filter=MainIngredient/ID eq guid'770d5720-9ae8-11e3-a5e2-0800200c9a66'
That would return only products that are linked, via MainIngredient, to the Ingredient with the ID you gave in the question.
I want to know the difference between following two statements related to datasets in delphi.
dsMyDataSet.ParamByName('ID').AsInteger := 1122; //If ID is integer
dsMyDataSet.ParamByName('ID').AsString := '1122'; //If ID is string
and
dsMyDataSet.ParamByName('ID').Value := 1122; //ID is string or integer
Do these statements carry same meaning? Does "value" implicitly converts integer into string?
The TParam.AsInteger property, for instance, set the value and the data type of the parameter. TParam.Value does the same, but TParam will decide which type will be mapped to the value inside the Variant and not always it´s the data type you would like.
I advise you to set values by using the AsXXX properties only, since you will be in control of the parameter's data type, what can save you from having parameter binding errors.
So, answering your final question: no, the values won´t be converted to the right data type, you have to set the data type by selecting the right property to assign the value.
I'm developing a personal project with grails in order to learn this powerful tool.
I have encountered this problem when adding two new fields (x,y) to my domain class "Post":
class Post {
long id;
Date creationDate;
String text;
byte[] image;
Style style;
long likeCount;
long dislikeCount;
User owner;
//coordinates on the wall
int x;
int y;
//this is probably to remove
static hasMany = [judgment : Judgment];
static constraints = {
text(nullable:true, maxSize:5000);
image(nullable:true, maxSize:1000000);
creationDate(nullable:true);
x(nullable:true);
y(nullable:true);
}
}
I added x and y after I create some data (Post records) on my postgres database.
But when I run-app in grails console this error comes up:
| Error 2012-03-04 12:04:23,670 [pool-5-thread-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table post add column x int4 not null
| Error 2012-03-04 12:04:23,672 [pool-5-thread-1] ERROR hbm2ddl.SchemaUpdate - ERROR: column "x" contains null values
this is very strange because i declared in constraint x,y to be nullable.. so why grails try to alter my table declaring x and y to be not null?
They're primitive int fields, so nullable doesn't make sense. You can't store a null value in the class instance, and if you have a null value in the database there's no sensible default conversion to an int value. You may consider 0 a sensible value for null, but 0 and null aren't equivalent in general.
If you want to allow null values for primitive numbers (int, long, etc.) or for boolean use the non-primitive Object classes Integer, Long, Boolean, etc. Another problem with primitive types is validation. Since they default to 0 (or false for boolean) you can't know whether the user chose 0 or false or if they didn't make a choice at all and you just have the default values from the constructor. So making them non-primitive leaves them null and you can know whether they made a choice or not.
Also, unrelated - you don't need to declare the id field, since Grails adds one for you anyway. And lose the semicolons ;)