I'm using C++ Builder XE6. I'm getting a UnicodeString as a parameter and I wish to check if the string is set to NULL, and not an empty string.
I've tried to do some simple compares to see if the param is null, but seem to be failing. I'm using the == operator which doesn't seem to be working, which makes me think it is overloaded.
I've tried:
if (searchString == NULL)
In the debugger view, it shows the value of { NULL } in the local variables. If I add the variable to the watch list, then it shows it has a property of "Data" which is NULL.
Any ideas on how I can properly do the compare?
There is no such thing as a NULL value for a UnicodeString. A string is a series of characters, but NULL is a pointer (well - actually it is a macro that evaluates to an int, 0, but it is supposed to be used to indicate null pointers if your compiler doesn't support nullptr).
Internally, the data member of UnicodeString is NULL when the string is empty, and non-NULL when the string has at least 1 character.
To check if the string is empty, use the IsEmpty() method, which checks if the data member is NULL or not. There is only one empty state; there is no distinction between "empty" and "null" like some languages have.
The value you see in the debugger is the internal data member of UnicodeString, it is not a part of UnicodeString's interface. When you see NULL in the debugger, you should treat it as being an empty string.
Related
I'm a student who's new in Swift.
While I'm studying about the Optional, I got curious about the keyword nil, so I tried some experiment with it. I'm using Swift version 5.5.
As you can see in the image below, if I assign nil to a optional variable (which I named 'name') and then print it with print(name) and print("(name)") (string interpolation), I got nil on the console in both cases. (Line 5, 9)
But when I print nil without the optional variable, which I printed it with print(nil) and print("(nil)"), I got an error on both cases. (Line 7, 11)
I expected them all to print out nil but they didn't.
I really want to know the difference between those cases.
The issue is that nil isn't just one single value, like null in Java/C#.
nil is syntactic sugar for Optional<Wrapped>.none. There's one different kind of value for every possible Wrapped. E.g. there is Optional<Int>.none and Optional<String>.none, and you can't assign from one to the other, because they're unrelated types.
When you use nil, the value of the Wrapped generic parameter is inferred from context, much like when you do:
let a = Optional<Int>.none // most explicit
let b: Optional<Int> = Optional.none // Generic Wrapped param inferred from the type annotation
let c: Optional<Int> = nil // Same inference
print takes an Any, so that doesn't give any relevant contextual type information. When you say print(nil), it's not clear what the typed of Wrapped should be. You might not care (because Optional<Int>.none and Optional<String>.none both print "nil"), but the type system can't leave it open ended, so it's a type error.
If you added contextual type information, it works, such as using a variable of a known type, or using a coercion:
print(nil as String?)
Interesting question actually. If you look at the Swift documentation:
Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”.
So think of it as nil is the absence of some value. As an example if you have:
var name: String?
name can either be a string value, or the absence of a string value.
So it makes no sense to try and print nil explicitly since you already know that it's the absence of a value, however, it makes sense to print name to check if it has a string value and in that case what it is, or if it has no string value.
The error message in XCode tells you exactly what happens: nil is not compatible with expected argument type Any - the print function expects a non-nil argument, as does the string interpolation with \(),
So I've been following along to a fireship course on dart and I got the error: "Null check operator used on a null value". Was just wondering why? Here is the code:
String? answer;
String result = answer!;
That's working exactly as intended.
The String? answer; declaration introduces a nullable variable with a current value of null.
The answer! expression reads that value, checks whether it's null, throws if the value is null, or evaluates to the non-null value if it isn't null.
Since the value is indeed null, that operation throws the error you see.
The ! is the "null check operator". It's used on the value null. The error message is actually quite precise.
To avoid that, make sure the answer variable has a non-null value when you use the ! operator on it. (Often you won't need the ! for local variables because assigning a non-null value to the variable also promotes the variable's type to non-nullable, but there are cases where the compiler can't see that assignment, for example if it happens in a callback function.)
I'm using this repository to familiarize myself with Amazon's Cognito user system. In the file lib/screens/signup_screen.dart, starting at line 27 there is this piece of code:
TextFormField(
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(labelText: "Email"),
controller: _emailController,
validator: (value) =>
!validateEmail(value) ? "Email is Invalid" : null,
),
However, since we have null safety in Dart since version 2.x, this piece of code fails with the error message: The argument type 'String?' can't be assigned to the parameter type 'String'.
What I got from that is that value may not be equal to null and the code cannot guarantee that as it is. Please correct me if I'm wrong and I'm also hoping someone can explain to me why the code cannot guarantee null safety here. If the textfield is empty, value should be equal to "" instead of null.
Anyhow, I decided to use the ternary operator to fix this issue:
!validateEmail(value == null ? "" : value) ? ...
Which the IDE suggested I correct to:
!validateEmail(value ?? "") ? ...
Dart also suggested to insert a null check as another alternative:
!validateEmail(value!) ? ....
Up till now, I have just been using these fixes as a workaround to produce code fast without trying to understand what's actually going on.
So what's the difference between these methods? Does the ?? in the second method have an integrated == null check and is it exactly the same as writing value == null ? "" : value?
Also, what does the nullcheck value! do? Yes, it checks if value is equal to null, but what does it do for me when value is in fact equal to null?
The ?? is a null-aware operator , which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right:
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
Which is the same as doing a ternary operator but more readable.
The bang operator ! casts aways the nullability of the variable, meaning YOU explicitly say that the value CAN'T be null.
Of course, like any cast, using ! comes with a loss of static safety.
The cast must be checked at runtime to preserve soundness and it may
fail and throw an exception.
For further reading check out the documentation.
You can start learning more about sound null safety in Dart here: Understanding Null Safety
The null assertion value! is equal to casting to a non-nullable type: value as String. It raises an exception if value is null.
The conditional ?? operator acts as you expect: value ?? "" is a shorthand equivalent for value != null ? value : ""
What I got from that is that value may not be equal to null and the code cannot guarantee that as it is.
value might be null, but validateEmail requires that its argument is not null.
can explain to me why the code cannot guarantee null safety here. If the textfield is empty, value should be equal to "" instead of null.
Because that's what TextFormField's constructor specifies. Its validator parameter is declared to be a FormFieldValidator<String>?, and FormFieldValidator<T> is defined to be a String? Function(T? value). In other words, the validator callback is declared to be a function that must accept null as an argument. The compiler does not have semantic knowledge to know whether TextFormField will actually invoke that callback with null instead of an empty string in practice.
So what's the difference between these methods? Does the ?? in the second method have an integrated == null check and is it exactly the same as writing value == null ? "" : value?
?? is one of Dart's null-aware operators; it automatically checks for null for you. As stated in the Dart Language Tour:
expr1 ?? expr2
If expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2.
So yes, it is equivalent to expr1 == null ? expr2 : expr1 (except that ?? would evaluate expr1 only once, which matters if evaluation has side effects). You should prefer ?? anyway because it better conveys intent.
Also, what does the nullcheck value! do? Yes, it checks if value is equal to null, but what does it do for me when value is in fact equal to null?
value! will throw an exception (specifically a TypeError) if value turns out to be null at runtime. Do not use ! unless you can logically guarantee that value will not be null.
I am a newcomer to coding in general and I want to learn basic Lua scripting for my own hobby.
After working on a Lua script, the syntax all seems to be without error but I have come across one issue that I don't understand, I broke it down to this basic function:
{$lua}
ID1 = "10"
ID2 = "0"
if (ID1 ~= nil and ID1 == "10") then
writeInteger(ID2,"25")
end
print(ID2)
The issue is that the writeInteger does not seem to work at all, ID2 remains at value "0" while it should become "25".
Any help would be greatly appreciated.
This is not valid Lua, or at least it isn't valid vanilla (out-of-the-box) Lua, and since you have not specified anything else, there is not much we can do to help. I will assume writeInteger is a valid function (since your interpreter isn't complaining about a call to a nil value), but I don't think it works as you expect.
If you want to set the ID2 variable to 25, simply write:
ID2 = 25
Lua will convert the string type to an integer type automatically. You can run print(type(ID2)) to confirm this
If you are using cheat engine (as a quick google search suggests) the writeInteger function requires an address and a value.
function writeInteger(Address, Value): Boolean - Returns true on success.
I am not sure if ID2, a Lua variable, is a valid address, but I am sure that "25" is not an integer. You should remove the quotation marks to start, and since the function returns a boolean you can see if the function was successful by doing:
print(writeInteger(ID2, 25))
Lua uses pass by value for primitive types like numbers, booleans and strings. So if you pass a number to a function like writeInteger, it creates a local copy within the function. The function can alter this copy but it will have no effect on caller (of writeInteger in this case). I don't know how writeInteger is supposed to work but if you want to call a function which alters its argument you can create a table and pass that. Tables are still passed by value but the "value" of a table is its memory address (so in effect tables are passed by reference and you can alter the contents of a table by passing it to a function).
See more here
Function/variable scope (pass by value or reference?)
I have a table with TEXT, INTEGER, REAL and other data types.
I wrote a generic sql function to read results for all queries using sqlite3_column_text() like so:
char *dataAsChar = (char *) sqlite3_column_text(compiledStatement, ii);
Although I should be using sqlite3_column_int etc. to read the numeric values, the above code seems to work for me. I get the number values as string which I later convert to int using [*numberAsString* intValue].
Since I am using a generic function to read all my db values, this is very convenient for me. But is there something that can go wrong with my code?
I could use sqlite3_column_type for each column to determine the type and use appropriate function. Am I correct in assuming that sqlite3_column_text basically returns the column value in TEXT format and does not necessarily need the value itself for be TEXT?
The only situation where I can see this implementation failing is with BLOB data type.
The documentation says:
These routines attempt to convert the value where appropriate. […]
The following table details the conversions that are applied:
Internal Type Requested Type Conversion
NULL TEXT Result is a NULL pointer
INTEGER TEXT ASCII rendering of the integer
FLOAT TEXT ASCII rendering of the float
BLOB TEXT Add a zero terminator if needed