How can i delete this unresolved reference? - kotlin

I am looking some help because I've tried to recover some picture with that code for a recycler View:
int images [ ] = {
R.drawable.lower_legs; R.drawable.upper_legs; R.drawable.tronc
R.drawable.back; R.drawable.arm_and_shoulder
}
but two errors appears : the first one is : unresolved reference with my int, and the second one is Variable expected. I would to recover these pictures with that code in my adapter :
holder.muscleImage.setImageResource(images[position])
How can I have a right code ? Thanks all !

val images = listOf(
R.drawable.lower_legs, R.drawable.upper_legs, R.drawable.tronc,
R.drawable.back, R.drawable.arm_and_shoulder
)
I don't want to extrapolate without knowing more context, but it could be argued that the syntax problem you are having is due to not knowing the basic Kotlin syntax.
Variable expected
Everything has to be, a file, a class, a variable or a function. So when you are assigning int images [] = ... you are not doing anything because the correct way to assign a variable is starting the declaration with the reserved word val.
unresolved reference with my int
In Kotlin the type is Int, a lower case might be caused by a Java confusion. And the type you need there is List<Int>. So by saying int you would be trying to assign a single number to a collection of numbers.
As a general recommendation learning new languages just by jumping on a project can be hard, taking an afternoon to read the basic syntax smooths the learning curve.
For Kotlin I would strongly recommend doing the Koans also TutorialPoints is short and straight to the point.

Related

How can we get rid of import syntax error in IntelliJ IDEA for Kotlin?

This code is a basic input/output function in Kotlin. But I am facing an issue with syntax even though it is right.
I figured out the mistake here. first of i wanted to take an input an integer type and also a string type which must be provided by the user. so i tried to use scanner instead we could have used val a: Int =readLine()!!.toInt() for integer type. and val b:string=readLine()!! for string type.

Unexpected tokens I'm following a tutorial and we do really the same code but the video was published on 2018

Log.d(tag:"MainActivity", msg:"Email is: " + email)
Log.d(tag:"MainActivity", msg:"Password: $password")
Log.d("MainActivity", "Email is: $email");
Log.d("MainActivity", "Password: $password")
The problem is, that you copied the so-called "Parameter Hints" by IntelliJ.
The parts of your code that end with a double-colon (tag: and msg:) aren't really part of the Kotlin code. Instead, the IDE that the creator of the tutorial was using showed hints for the names of the parameters.
Example
The function
fun add(a: Int, b: Int): Int {
return a + b
}
has two parameters, the first called a and the second called b.
This means when calling this code from IntelliJ, the IDE will show the following hints to the user so that he knows what parameters he is entering:
You can identify these hints by looking at them accurately. They have a different font, are boxed, and have a different background-color. Please note that these hints are not part of the Kotlin code and cannot be written in Kotlin, that's where your Syntax Error comes from.
Have fun learning Kotlin! 👍

Kotlin collections map causing a 'Type Checking Has Run Into A Recursive In Kotlin' error

I have 2 classes lets call them A and B, I also have a function that converts an instance of A to an instance of B.
My code that is causing issues is basically:
fun fromAtoB(a: A) = B (fb1 = a.fa1, fb2 = a.fa2, fb3 = a.fa3)
val listOfA: List<A> = ...
val listOfB: listOfA.map { fromAtoB(it) }
This won't build due to the line:
fromAtoB(it)
With the error:
Due to the error Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly
I Have no clue what I can do to fix this, Google had provided no results that seem to apply to my issue...
Thanks in advance for any help!
EDIT:
Here are the actual source files:
TenantEntity.kt - https://pastebin.com/mdSWiA1Y (Line 51 of this file
is the issue)
TenantDto.kt - https://pastebin.com/83UP9Cwe
ReceiptEntity.kt - https://pastebin.com/BjP2ikg9
ReceiptDto.kt - https://pastebin.com/Kpt9dSAp
This type of problem means that the compiler can't infer what the type of listOfB should be, because you have a recursive call somewhere in its definition. That's curious as I can't see any in your example code, but maybe you left out the offending code inadvertently. Anyways, this problem is usually resolved by doing what the error message suggests, manually specifying the return type like so:
val listOfB: List<B> = listOfA.map { fromAtoB(it) }
Edit:
After trying the real code, I just went after a hunch and changed line 45 in TenantEntity.kt from this:
fun fromDto(dto: TenantDto) = TenantEntity (
to this:
fun fromDto(dto: TenantDto): TenantEntity = TenantEntity (
and the error was gone. I'm not really sure why, but it should compile now.
Second edit:
Upon further inspection, you're going to run into a StackOverflowException with this code, which is ultimately why the compiler couldn't resolve the type. When you call TenantEntity.fromDto(...), that will call ReceiptEntity.fromDto(...), which will in turn call TenantEntity.fromDto(...), and back again, into eternity (or the stack limit). That's not going to work, you'll need to fix your logic there.

how to find out if a c++/cli heap variable has <undefined value>

I've not been using C++ for about 4 years and came back to it a month ago, and that was where I also have first heard about the CLI extension. I still have to get used to it, but this website helps a lot! Thank you!! Anyway, I couldn't find an answer to the following problem:
When I declare a variable
int iStack;
then it is declared but not defined, so it can have any value like
iStack = -858993460
depending on what the value at the stack position is, where the variable is created.
But when I declare a variable on the heap
int^ iHeap
then as far as I know the handle is created but the variable is not instantiated (don't know if you call it instantiation here) or defined and I can only see
iHeap = <Nicht definierter Wert> (which means <undefined value>)
Is there any way to detect if this value is defined?I particularly don't need it for int, but for example for
array<array<c_LocationRef^,2>^>^ arrTest2D_1D = gcnew array<array<c_LocationRef^,2>^>(2);
to find out if the elements of the outer or inner array are instantiated (I'm sure here it is an instantiation ;-) )
arrTest2D_1D = {Length=2}
[0] = {Length=20}
[1] = <Nicht definierter Wert> (=<undefined value>)
As far as I know, the CLR automatically initialise your variables and references in C++ CLI.
In .NET, the Common Language Runtime (CLR) expressly initializes all
variables as soon as they are created. Value types are initialized to
0 and reference types are initialized to null.
To detect if your variable is initialised, you should compare the value of your hat variable to nullptr :
int^ iHeap;
if(iHeap == nullptr){
Console::WriteLine(L"iHeap not initialised");
}
This works on my VS2010 ; it outputs iHeap not initialised
It should work for your specific problem as well (arrays).
By the way, value types are initialised to zero hence your first example should output 0 (I've tested it, and it does output 0) :
int iStack;
Console::WriteLine(L"iStrack = {0}", iStack); // outputs 0
Quote is from codeproject
MSDN page for nullptr
EDIT: Here is an other quote, from Microsoft this time :
When you declare a handle it is automatically initialized with null, so it will not refer to anything.
Quote from MSDN see the paragraph "Tracking Handles"

Write a compiler for a language that looks ahead and multiple files?

In my language I can use a class variable in my method when the definition appears below the method. It can also call methods below my method and etc. There are no 'headers'. Take this C# example.
class A
{
public void callMethods() { print(); B b; b.notYetSeen();
public void print() { Console.Write("v = {0}", v); }
int v=9;
}
class B
{
public void notYetSeen() { Console.Write("notYetSeen()\n"); }
}
How should I compile that? what i was thinking is:
pass1: convert everything to an AST
pass2: go through all classes and build a list of define classes/variable/etc
pass3: go through code and check if there's any errors such as undefined variable, wrong use etc and create my output
But it seems like for this to work I have to do pass 1 and 2 for ALL files before doing pass3. Also it feels like a lot of work to do until I find a syntax error (other than the obvious that can be done at parse time such as forgetting to close a brace or writing 0xLETTERS instead of a hex value). My gut says there is some other way.
Note: I am using bison/flex to generate my compiler.
My understanding of languages that handle forward references is that they typically just use the first pass to build a list of valid names. Something along the lines of just putting an entry in a table (without filling out the definition) so you have something to point to later when you do your real pass to generate the definitions.
If you try to actually build full definitions as you go, you would end up having to rescan repatedly, each time saving any references to undefined things until the next pass. Even that would fail if there are circular references.
I would go through on pass one and collect all of your class/method/field names and types, ignoring the method bodies. Then in pass two check the method bodies only.
I don't know that there can be any other way than traversing all the files in the source.
I think that you can get it down to two passes - on the first pass, build the AST and whenever you find a variable name, add it to a list that contains that blocks' symbols (it would probably be useful to add that list to the corresponding scope in the tree). Step two is to linearly traverse the tree and make sure that each symbol used references a symbol in that scope or a scope above it.
My description is oversimplified but the basic answer is -- lookahead requires at least two passes.
The usual approach is to save B as "unknown". It's probably some kind of type (because of the place where you encountered it). So you can just reserve the memory (a pointer) for it even though you have no idea what it really is.
For the method call, you can't do much. In a dynamic language, you'd just save the name of the method somewhere and check whether it exists at runtime. In a static language, you can save it in under "unknown methods" somewhere in your compiler along with the unknown type B. Since method calls eventually translate to a memory address, you can again reserve the memory.
Then, when you encounter B and the method, you can clear up your unknowns. Since you know a bit about them, you can say whether they behave like they should or if the first usage is now a syntax error.
So you don't have to read all files twice but it surely makes things more simple.
Alternatively, you can generate these header files as you encounter the sources and save them somewhere where you can find them again. This way, you can speed up the compilation (since you won't have to consider unchanged files in the next compilation run).
Lastly, if you write a new language, you shouldn't use bison and flex anymore. There are much better tools by now. ANTLR, for example, can produce a parser that can recover after an error, so you can still parse the whole file. Or check this Wikipedia article for more options.