brand new, and have a question; we were going over...
# announcements
t
brand new, and have a question; we were going over something in class and I wasn't able to get an answer; Can someone please tell me what the difference is between the first two photos and the last two photos? They use the same methods of interpolation, but my instructor was trying to emphasize that the first two won't work because the interpolation is happening within the println statement... but the last images are proof that it does work inside the println statement; I am so confused.
the displayMoreContent function is no different than the displayDiet function....
a
Return type seems to be different judging by the output
Your return is basically printed out with toString() call , apparently getMoreContent returned Unit
t
The displayMoreContent is what she demo'd in class and went on with it for 25 minutes asking us why it won't work...
the bear.displayDiet is what was on one of our assignments...but it worked out?
a
👀 define "will not work". It did work just the return type of displayMoreContent was Unit and not String 🤷 imo
Appearanly it compiled and executed so not sure how to understand it will not work
Do you have the source code for the displayMoreContent ?
t
sorry, meant not working as in its displaying backwards. It will display properly (in order) if you put the myInterfaceObject.displayMoreContent() on its own line. She wanted us to answer why we couldn't put it all in one println statement. I said to throw in concatenation as an option, and it still didn't compile as expected;
a
Got ya , can you show displayDiet method
So I can bet that displayDiet actually returns string , the reason why you are seeing this "weird" output is that the expression is evaluated first then the result is displayed
t
image.png
a
As the expression actually prints something itself the code prints something from the expression first and then prints the result of your template which has this weird Unit at the end
👍 1
Yeap has String as the return type
As I said first the code inside the expression is executed and then the template is concatenated and printed
So if you make a change into displayMoreContent and have it like so fun displayMoreContent() = "more content"
It is gonna print the string "in order"
🤩 1
Just to clarify the expression means the part after the $ in { }
t
THANK YOU
a
No problem