I want to write code like msg = result ? msg_v : msg_x in Lua.
I tried to do something like:
result = true
msg_v = "true!"
msg_x = "false!"
msg = (msg_v and result) or msg_x
print(msg)
but I get true as result and not true!. how can I fix that?
Check out my Version (colored)
€ lua
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
> msg_v, msg_x = "true!", "false!"
> result = true
> msg = not result and ('\27[1;31m%s: %s\27[m'):format(type(result),msg_x) or ('\27[1;32m%s: %s\27[m'):format(type(result),msg_v)
> print(msg)
boolean: true!
> result = false
> msg = not result and ('\27[1;31m%s: %s\27[m'):format(type(result),msg_x) or ('\27[1;32m%s: %s\27[m'):format(type(result),msg_v)
> print(msg)
boolean: false!
>
Impression
Formating False in blinking red Capital Letters do...
msg = not result and ('\27[1;5;31m%s: %s\27[m'):format(type(result):upper(), msg_x:upper()) or ('\27[1;32m%s: %s\27[m'):format(type(result), msg_v)
...have fun ;-)
PS: Check out the special (datatype) Cases and learn how to catch them
> result = "I am a String"
> msg = not result and ('\27[1;5;31m%s: %s\27[m'):format(type(result):upper(), msg_x:upper()) or ('\27[1;32m%s: %s\27[m'):format(type(result), msg_v)
> print(msg)
string: true!
> result = {"I am a String in a Table"}
> msg = not result and ('\27[1;5;31m%s: %s\27[m'):format(type(result):upper(), msg_x:upper()) or ('\27[1;32m%s: %s\27[m'):format(type(result), msg_v)
> print(msg)
table: true!
> result = print -- Just a Function
> msg = not result and ('\27[1;5;31m%s: %s\27[m'):format(type(result):upper(), msg_x:upper()) or ('\27[1;32m%s: %s\27[m'):format(type(result), msg_v)
> print(msg)
function: true!
> result = io.stdin -- User Data
> msg = not result and ('\27[1;5;31m%s: %s\27[m'):format(type(result):upper(), msg_x:upper()) or ('\27[1;32m%s: %s\27[m'):format(type(result), msg_v)
> print(msg)
userdata: true!
...and...
> result = math.deg(math.pi)
> msg = not result and ('\27[1;5;31m%s: %s\27[m'):format(type(result):upper(), msg_x:upper()) or ('\27[1;32m%s: %s\27[m'):format(type(result), msg_v)
> print(msg)
number: true!
...in the meantime you are fine with not if result...
> result = nil
> msg = not result and ('\27[1;5;31m%s: %s\27[m'):format(type(result):upper(), msg_x:upper()) or ('\27[1;32m%s: %s\27[m'):format(type(result), msg_v)
> print(msg)
NIL: FALSE!
...simply not exists.
Related
As the title, when I create the pdf, accented characters are not shown or appear this ì, how can I solve? I already tried with several encoding, but doesn't works.
Thanks lot in advance
PoDoFo::PdfFont* pFont = document->CreateFont("Liberation Serif", false, false, false, PoDoFo::PdfEncodingFactory::GlobalStandardEncodingInstance());
tablemodelint->SetFont(pFont);
if(dataSel1.GetWeekDay() == wxDateTime::WeekDay::Mon)
giorno = "Lunedì";
else if(dataSel1.GetWeekDay() == wxDateTime::WeekDay::Tue)
giorno = "Martedì";
else if(dataSel1.GetWeekDay() == wxDateTime::WeekDay::Wed)
giorno = "Mercoledì";
else if(dataSel1.GetWeekDay() == wxDateTime::WeekDay::Thu)
giorno = "Giovedì";
else if(dataSel1.GetWeekDay() == wxDateTime::WeekDay::Fri)
giorno = "Venerdì";
else if(dataSel1.GetWeekDay() == wxDateTime::WeekDay::Sat)
giorno = "Sabato";
else if(dataSel1.GetWeekDay() == wxDateTime::WeekDay::Sun)
giorno = "Domenica";
tablemodelint->SetText(1, 0, giorno + ", " + dataSel1.Format("%d-%m-%y").ToStdString());
My code is as below
private fun validateInput(): Boolean {
if (etReportRow1.text.toString() == ""
|| etReportRow2.text.toString() == ""
|| etReportRow3.text.toString() == "")
return false
else
return true
}
The compiler tell me
Redundant 'if' statement less... (Ctrl+F1) This inspection reports if
statements which can be simplified to single statements. For example:
if (foo()) { return true } else { return false } can be
simplified to return foo().
Won't the suggested code go into loop?
All statments in the form:
if(condition){
return false
} else {
return true
}
can be simplified into:
return !condition
So in your case it would lead to:
return !(etReportRow1.text.toString() == "" || etReportRow2.text.toString() == "" || etReportRow3.text.toString() == "")
Or:
return
etReportRow1.text.toString().isNotEmpty() &&
etReportRow2.text.toString().isNotEmpty() &&
etReportRow3.text.toString().isNotEmpty()
Note: the isNotEmpty() is an extension method:
public inline fun CharSequence.isNotEmpty(): Boolean = length > 0
To avoid duplicate code you could also use a Sequence:
public fun validateInput() = sequenceOf(etReportRow1, etReportRow2, etReportRow3)
.map { it.text.toString() }
.all { it.isNotEmpty() }
Because a boolean expression evaluates to a boolean value, you can simply return the result of the expression itself, without having to explicitly return true or false.
You can further simplify things using the following single-expression function:
private fun validateInput() = etReportRow1.text.toString() != "" &&
etReportRow2.text.toString() != "" &&
etReportRow3.text.toString() != ""
try
return !(etReportRow1.text.toString() == "" || etReportRow2.text.toString() == "" || etReportRow3.text.toString() == "")
I think this should work:
private fun validateInput() = !(etReportRow1.text.toString.isEmpty()
|| etReportRow2.text.toString().isEmpty()
|| etReportRow3.text.toString().isEmpty() )
Even more concise:
public fun validateInput() = setOf(
etReportRow1, etReportRow2, etReportRow3
).none {
"${it.text}".isEmpty()
}
I have this code:
val leftEnoughRoom = if(fx1 > eachSideBesidesFace){
true
}else{
false
}
And get the warning:
This inspection reports if statements which can be simplified to single statements. For example:
if (foo()) {
return true
} else {
return false
}
can be simplified to return foo().
What does it want me to do? When I do:
if(fx1 > eachSideBesidesFace){
val leftEnoughRoom = true
}else{
val leftEnoughRoom = false
}
Then leftEnoughRoom is not reachable below any more
fx1 > eachSideBesidesFace
is a boolean statement. You don't need the if-else:
val leftEnoughRoom = fx1 > eachSideBesidesFace
As a sidenote, you can click the underlined expression, hit Alt+Enter and then have Android Studio automatically optimize the code.
I am using the Cassandra C++ driver in my application. I am observing many crashes. After debugging I identified that even when the query output is zero rows , the if (result == NULL) is false and when I iterate through the result, one place or other it is crashing. Below is the code sample. Please suggest me any solution for this.
const char* query = "SELECT variable_id, variable_name FROM aqm_wfvariables WHERE template_id = ?;";
CassError rc = CASS_OK;
CassSession* session = NULL;
if((session=CassandraDbConnect::getInstance()->getSessionForCassandra())==NULL){
return false;
}
CassStatement* statement = cass_statement_new(query, 1);
cass_statement_bind_int32(statement, 0, wf_template_id );
CassFuture* query_future = cass_session_execute(session, statement);
cass_future_wait(query_future);
rc = cass_future_error_code(query_future);
if (rc != CASS_OK) {
logMsg(DEBUG, 7, "cass_session_execute failed for query #%d:%s:%s", 1, __FILE__, query);
cass_statement_free(statement);
return false;
}
cass_statement_free(statement);
const CassResult* result = cass_future_get_result(query_future);
if (result == NULL) {
cass_future_free(query_future);
logMsg(DEBUG, 7, "No values are returned for query #%d:%s:%s", 1, __FILE__, query);
return false;
}
cass_future_free(query_future);
CassIterator* row_iterator = cass_iterator_from_result(result);
while (cass_iterator_next(row_iterator)) {
const CassRow* row = cass_iterator_get_row(row_iterator);
/* Copy data from the row */
You should use
(result.cass_result_row_count>0)
instead of
(result == NULL)
to verify if query returns zero rows. In your code, result is always an instance of CassResult and not a null reference when zero rows are returned.
I tried to uset IDataObject to put a some text to clipboard. But the GlobalUnlock fails. What I'm doing wrong?
IDataObject *obj;
HRESULT ret;
assert(S_OK == OleGetClipboard(&obj));
FORMATETC fmtetc = {0};
fmtetc.cfFormat = CF_TEXT;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
STGMEDIUM medium = {0};
medium.tymed = TYMED_HGLOBAL;
char* str = "string";
medium.hGlobal = GlobalAlloc(GMEM_MOVEABLE+GMEM_DDESHARE, strlen(str)+1);
char* pMem = (char*)GlobalLock(medium.hGlobal);
strcpy(pMem,str);
assert(GlobalUnlock(medium.hGlobal) != 0); // !!! ERROR
assert(S_OK == obj->SetData(&fmtetc,&medium,TRUE));
assert(S_OK == OleSetClipboard(obj));
Well, after looking at the documentation this is to be expected:
Return Value
If the memory object is still locked after decrementing the lock count, the return value is a nonzero value. If the memory object is unlocked after decrementing the lock count, the function returns zero and GetLastError returns NO_ERROR.
If the function fails, the return value is zero and GetLastError returns a value other than NO_ERROR.
So your assertion is wrong; it should be:
assert(GlobalUnlock(medium.hGlobal) == 0 && GetLastError() == NO_ERROR);