java


Lambda expressions - can not set lambda parameter as argument to method


I'm trying use lambda expressions on Android using retrolambda. In code below I need to add listener that is interface:
public interface LoginUserInterface {
void onLoginSuccess(LoginResponseEntity login);
void onLoginFail(ServerResponse sr);
}
code
private void makeLoginRequest(LoginRequestEntity loginRequestEntity) {
new LoginUserService(loginRequestEntity)
.setListener(
login -> loginSuccess(login),
sr -> loginFail(sr))
.execute();
}
private void loginSuccess(LoginResponseEntity login) {
//TODO loginSuccess
}
private void loginFail(ServerResponse sr) {
//TODO loginFail
}
But Android Studio marks red loginSuccess(login) and loginFail(sr) as mistakes
and shows message "LoginResponseEntity cannot be applied to " and "ServerResponse cannot be applied to "
So I can not set lambda parameter 'login' as argument to method loginSuccess(login).
Please help me to understand what's wrong with this expression.
You can use lambdas only with Functional interfaces. It means that your interface has to specify only one method.
To remember about it (simply - to have the ability of using lambdas instead of anonymous classes), the best is to put #FunctionalInterface annotation to your interfaces.
#FunctionalInterface
public interface LoginUserInterface {
LoginResult login(...)
}
and then dispatch on the value of LoginResult
Yes, correct answer is "You can use lambdas only with Functional interfaces. It means that your interface has to specify only one method."
For other who will search for some workaround this is my solution:
Devide interface on two functional interfaces
public interface SuccessLoginUserInterface {
void onLoginSuccess(LoginResponseEntity login);
}
public interface FailLoginUserInterface {
void onLoginFail(ServerResponse sr);
}
And your lambda expression will look well:
private void makeLoginRequest(LoginRequestEntity loginRequestEntity) {
new LoginUserService(loginRequestEntity)
.setsListener(
login -> loginSuccess(login),
sr -> loginFail(sr))
.execute();
}

Related Links

Server to Client Notification / Web service(jax-ws) to client communication
Break [q0/q1] into [q1,q2] [closed]
N-Puzzle pseudo-random shuffling?
How to show the following classes and their associations in UML?
Log4j failing to send an email when logging an error
GPS Devices and Programming
problem with sockets in java
JSP with CSS does not display the background image
Execution priority of expressions
background process using thread
Convert file .pptx to image using Java
How to design a threaded UDP client/server in Java?
HTML with JS to Image/PDF
How to generate strongly typed schema in Metro
How to define static Arraylist in thread safe environment
exception in thread 'main' java.lang.NoClassDefFoundError:

Categories

HOME
vim
wso2-am
deployment
omnet++
activiti
plone
homebrew
fft
sd-card
infragistics
c#-2.0
disassembler
directx
autotools
slurm
awesome-wm
gnupg
tomcat6
node-pdfkit
mapserver
etl
postgres-xl
resize
php-7.1
commonmark
dynamics-crm-online
highlight.js
beyondcompare
visual-composer
graphicsmagick
captiveportal
tdd
apache-commons-io
brunch
jspm
libssl
quote
google-cloud-nl
gsmcomm
http-status-code-503
karaf
jspresso
http-get
perlin-noise
openoffice.org
rotational-matrices
http-referer
android-ble
stacked
bitbucket-pipelines
hot-module-replacement
vapor
leading-zero
graphenedb
optix
avro4s
google-closure
node-gyp
ibpy
nand2tetris
nomethoderror
arena-simulation
modelmapper
flashair
gpx
logfiles
parentheses
apache-fop
strptime
errordocument
paxos
tizen-native-app
nodebb
font-size
google-perftools
media-player
eventkit
word-vba-mac
xml-attribute
bstr
fuzzy-search
fputcsv
angstrom-linux
two-factor-authentication
xpath-1.0
icu4j
collapse
retina
0xdbe
pretty-print
dereference
pick
dd
winddk
java.util.concurrent
undefined-reference
fmod
imdbpy
industrial
soundtouch
ember-app-kit
reddot
hgsubversion
window-management
disclosure
celltable
lang
html-input
google-friend-connect
.nettiers
iweb
boost-smart-ptr
spec#
ajax-forms

Resources

Encrypt Message