Why is this lambda illegal?

hey guys. can smb explain lambdas to me? what am i doing wrong?
public interface FunctionalInterface {
String function(String str);
}
public class Main {
public static void main(String[] args) {
FunctionalInterface functionalInterface= "hello"->"hello";
}
}
public interface FunctionalInterface {
String function(String str);
}
public class Main {
public static void main(String[] args) {
FunctionalInterface functionalInterface= "hello"->"hello";
}
}
My lambda complies to the functional interface. Its argument is of String type, and it returns String too. wt f?
11 Replies
JavaBot
JavaBot14mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
0x150
0x15014mo ago
what do you think that would do / what was your intention when testing that out
i hate SQL so much its unreal
well my lambda has to comply to the method signature of functional interface . method has to accept string, and return string. i give lambda a string, and it screams at me
No description
0x150
0x15014mo ago
at this point, you dont give the lambda a string you need to define the lambda giving it a string comes later defining a lambda is the same as a method but in a slightly different format and without an inherent name: (arguments) -> expression or (arguments) -> { methodBody; return expression; } since FunctionalInterface is your target lambda type, your lambda should accept one string as parameter and return another. so your lambda should look something like this: theStringArgument -> theStringArgument.doSomethingWithAString() (as an example) that makes a new lambda that takes in one string (stored under theStringArgument), calls the imaginary doSomethingWithAString() on it and returns its return value (a string) to then use that lambda, you do String result = functionalInterface.function("some string")
i hate SQL so much its unreal
@0x150 do i understand it correctly?
String greeting = "hi";
FunctionalInterface functionalInterface= myGreeting-> "hello";// this is basically an impl of interface, not a method call
functionalInterface.function(greeting);//actual method call
String greeting = "hi";
FunctionalInterface functionalInterface= myGreeting-> "hello";// this is basically an impl of interface, not a method call
functionalInterface.function(greeting);//actual method call
0x150
0x15014mo ago
correct that is exactly what is happening the lambda is just an implementation of the interface method lambdas only work if the target interface has exactly one abstract method, for that exact reason
i hate SQL so much its unreal
okay. but then the question is why doesnt ide scream at me for myGreeting? because it isnt described anywhere
0x150
0x15014mo ago
where the parameter? its a parameter, its not a predefined variable in fact, defining a variable with the same name causes an error because the variable already exists what you're defining there is a method parameter it's a slot an input argument gets stored in, in this case the string it applies the operation to if you already knew the value and lambdas would only be able to apply to one specific value once, they'd be a bit useless, no?
i hate SQL so much its unreal
yup yup. it would be the same as if i had a getter for an object, but it would always return hardcoded name
JavaBot
JavaBot14mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?