Why is this lambda illegal?
hey guys. can smb explain lambdas to me? what am i doing wrong?
My lambda complies to the functional interface. Its argument is of String type, and it returns String too. wt f?
11 Replies
⌛
This post has been reserved for your question.
Hey @bambyzas! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
what do you think that would do
/ what was your intention when testing that out
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

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")
@0x150 do i understand it correctly?
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
okay. but then the question is why doesnt ide scream at me for
myGreeting
?
because it isnt described anywherewhere
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?
yup
yup. it would be the same as if i had a getter for an object, but it would always return hardcoded name
💤
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.