Imagine I have some function like this:
fun dummyFunction():Int{
if(True){
val a = 1
} else {
val a = 2
}
return a
}
Stack trace (from LeetCode):
Line 7: Char 11: error: unresolved reference: a
return a
^
Why this code gives unresolved reference: a error? Every help is appreciated
P.S. I know how to solve this. But I got curious about this very situation
This error is for scope of variables
The "a" variable just exist in {} of the if and the else
Way to fix this problem is define the "a" in out of if-else
Related
I'm new to Zig and am trying to learn how error-handling and error sets work.
If I run
const erro = error{Oops};
fn failingFunction() erro.Oops!void {
return erro.Oops;
}
test "returning an error" {
failingFunction() catch |err| {
try expect(err == erro.Oops);
return;
};
}
I get an error:
error: expected type 'type', found 'erro'
fn failingFunction() erro.Oops!void {
^
./test.zig:45:31: note: referenced here
fn failingFunction() erro.Oops!void {
^
./test.zig:50:5: note: referenced here
failingFunction() catch |err| {
But when i use erro!void instead of erro.Oops!void as the funtion return type, the tests pass. Why is this so?
Please help. How do error unions work in the language? Thank You.
EDIT: The above is a modified function. The original function is
fn failingFunction() error{Oops}!void {
return error.Oops;
}
from this article: https://ziglearn.org/chapter-1/ in the "Errors" section. I wanted to experiment and so out of curiosity I did the above.
in failingFunction exception you have passed an error option (Oops) not error type. that's useless and wrong. overwrite your function with:
fn failingFunction() erro!void {
return erro.Oops;
}
I know there are a lot of questions about this exception but no answer suited my case.
var count = sharedPref.getInt("flutter.badgeCount", 0) // line 12
ShortcutBadger.applyCount(applicationContext, count+1) // line 13
count should be an integer because sharedPref.getInt returns an integer, and applyCount() receive an integer as the second parameter. The exception is thrown at runtime at line 12.
Is there anything I can't see? (I'm pretty new to kotlin)
Use the following. This would solve the problem.
var count = sharedPref.getLong("flutter.badgeCount", 0L)
ShortcutBadger.applyCount(applicationContext, count.toInt()+1)
Does someone know what goes I'm doing wrong here?
// ...
val alphabet = ('a'..'z').toList()
val str = "testing"
val arr = str.split("")
for (char in arr) {
var i = alphabet.indexOf(char) // Throws Error!
// ...
}
The indexOf-method results in
"Error:(10, 26) Kotlin: Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly."
I have tried to "var i: Int = alphabet.indexOf(char)" and "alphabet.indexOf(char) as Int". The same result ...
What's the issue here?
I believe that your problem is that you think that the variable char contains a Char value. If that were the case, then it would make sense for indexOf to be accepting a Char value and then finding the location of that character in a list of characters (List<Char>) on which the method is being called.
But str.split() is returning a List<String>, not a List<Char>, and so char is a String. So you are asking the indexOf method to tell you the location of a String in a list of characters (List<Char>). That doesn't make sense. That's the basis of the problem. Exactly how that translates to the error you're getting from the Kotlin compiler, I'm not sure.
To make it clear, this line produces the same error:
var i = alphabet.indexOf("c") // Throws Error!
but the compiler likes this line just fine:
var i = alphabet.indexOf('c') // No Error!!!
I have "Conditional branch result of type ... is implicity cast of Any?" warning when i try to do something like this:
objects?.forEach {
val gson = Gson()
val chatObject = if(it.type == CHAT_TEXT_TYPE) gson.fromJson(it.value, ChatText::class.java) //WARNING
else gson.fromJson(it.value, ChatProduct::class.java) //WARNING
//TEST
if(chatObject is ChatText) Log.e("ChatText: It works!", chatObject.text)
if(chatObject is ChatProduct) Log.e("ChatProduct: It works!", chatObject.name)
}
But finally it works. Is anything wrong with my code? Is it any possibility to remove this warning?
EDIT (as #Xavier Bouclet suggested - warning disappeared)
val chatObject = when (it.type)
{
CHAT_TEXT_TYPE -> gson.fromJson(it.value, ChatText::class.java)
CHAT_PRODUCT_TYPE -> gson.fromJson(it.value, ChatProduct::class.java)
else -> gson.fromJson(it.value, Any::class.java)
}
The warning is telling you that you've returned different types in your branches, and that the variable you're assigning will be inferred to be their first common supertype, which in this case is Any?. This is not the way you usually use the if-else expression, you usually return the same type from both branches. The IDE is warning you because what you're doing is usually accidental.
If you're going to check the type of the result later for the actual types you're using anyway, you're probably fine with your code.
To remove the warning, you can either suppress the warning with a comment:
#Suppress("IMPLICIT_CAST_TO_ANY")
val chatObject = ...
Or explicitly state that the type that the if-else expression returns is unknown:
val chatObject: Any = ...
I write a pecl function named sample_isset, and its code is :
PHP_FUNCTION(sample_isset) {
zval **fooval;
if(EG(active_symbol_table) == NULL) {
php_printf("the active symbol table is NULL");
}else if(zend_hash_find(EG(active_symbol_table),"foo",4,
(void **) &fooval) == SUCCESS){
php_printf("Got the value of $foo!");
} else {
php_printf("$foo is not defined!");
}
}
And I want to use this function to see if current symbol table has a $foo variable.
When I use it in global scope, it works well.
But When I use it in another function for example hello, I go a error, and see nothing .
The hello function may be like this.
function hello(){
sample_isset();
}
I don't know why I got an error.
It seems that the php function used the lazy loading, the active_symbol_table doesn't always exist, so You should use zend_rebuild_symbol_table before EG(active_symbol_table) .