I'm reading "Programming in Lua" book and I don't understand exercise 2.1:
What is the value of the expression
type(nil)==nil?
(You can use Lua to check your answer.) Can you explain this result?"
When I execute this code I get "false" as result. I could not explain this result, from my point of view correct result should be "true". I tried
type(some_undeclared_variable)==nil
and it also gives me "false".
The function type() always returns a string, the value of type(nil) is the string "nil", which is not the same as nil, they have different type.
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 \(),
Curious about the syntax used in this example (https://learn.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-command-line) within the file Library.js
My question, is the getJson function returning multiple values without a tuple?
Any link to F# documentation that explains this syntax would be nice. thanks.
open System.Text.Json
let getJson value =
let json = JsonSerializer.Serialize(value)
value, json
My question, is the getJson function returning multiple values without a tuple?
Yes to the first part, no to the second. The comma on the last line makes these two values a tuple.
You may think from online examples that a tuple is like (1, 2), but it’s just as fine to remove the parentheses if the expression is only on one line. In this case, value, json is the tuple.
Parentheses are used to disambiguate the order of evaluation. For instance, 1, “two”, “three” is a three-tuple of an int and two strings, but 1, (“two”, “three”) is a two-tuple of an int and the 2nd type being another two-tuple of two strings.
The Microsoft Learning link appears to always use parentheses in the examples. This post goes a little further, and has a bit more to say on tuple deconstruction as well: https://fsharpforfunandprofit.com/posts/tuples/.
Here’s more on parentheses (thanks Brent!): if it has a comma, it’s a tuple.
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 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?)
What is the proper way to make a conditional which checks of something is or is not empty in Lua? if x == "" and f x ~= "" does not seem to work.
Lua is a dynamically type-based language.
Any variable can hold one of the following types: nil, boolean, number, string, table, function, thread, or userdata.
Any variable in a table (including _G, the table where globals reside) without a value gives a value of nil when indexed. When you set a table variable to nil, it essentially "undeclares" it (removing the entry from memory entirely).
When a local variable is declared, if it is not assigned immediately it is given a value of nil. Unlike table variable, when you set a local variable to nil, it does not "undeclare" it (it just has a value of nil).
In Lua, an empty string ("") is still a "value" - it's simply a string of size zero.
I recently ran across this problem as well. LuaSQL was returning empty strings if a database value was 'blank' (not null). A hacky approach, but here's how I solved it:
if (string.len(x) >= 1) then
...
end
I'm going to make an assumption that the OP means "how do you tell when a variable is unassigned".
Example:
local x
The variable x is "empty", it is initialized to "nil". (Not the text "nil", but an enumerated value that indicates that the variable is unassigned. In Lua that is defined as nil, in some other languages it is defined as NULL.)
Now assign x a value.
Example:
x=""
Now x is not nil.
Another example:
x=0
x is not nil.
Try running this code, it should make the situation clear to you.
local x
if x==nil then print("x is nil") end
x=0
if x==nil then print( "This line won't be written") end
x=""
if x==nil then print( "and this line won't be written") end
The first if statement will evaulate to true and the print statement will be called. The 2nd and 3rd if statements are false and the print statements will not be executed.
In conclusion, use "==nil" to check to see if a variable is "empty" (which is more properly said "nil").
You probably have spaces, newlines or other non-visible characters in your string. So you think it is "empty", but it isn't. This typically happens when you are taking input from the user, and has to type "enter" to finish - the "enter" ends up in the string.
What you need is a function that tells you whether the string is "blank" - either empty, or a list of spaces/tabs/newlines. Here's one way to do it:
function isBlank(x)
return not not tostring(x):find("^%s*$")
end
Usage:
if isBlank(x) then
-- ...
end