if(function(s)) { statement } (Language C) (Standard return) (+newbie) (+abreviated expressions) - return

how are you?
int stack_empty(stack *s){
return (s == NULL); /* I dont get this part, it returns what if its null? */
}
int main(){
stack *s;
if(stack_empty(s)){ /* what it means? like... whats the standard return of a function? */
printf("its empty");
}
return 0;
}
My questions are in the comments of the code. Briefly they are:
-> Whats the standard return of a function?
-> What does return something == NULL means?
*I know what NULL, s or == means... my questions lies on those abreviated expressions.

== is a test for equality.
If s is null then s == null is true the expression has a non zero value, so stack_empty will return non zero (probably 1). If s is not null, then s == null is false so the method will return 0.
The if statement is effectively saying if expression is not 0.

Related

Is 'none' one of basic types in Lua?

The basic types defined in Lua as below :
/*
** basic types
*/
#define LUA_TNONE (-1)
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2
#define LUA_TNUMBER 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TFUNCTION 6
#define LUA_TUSERDATA 7
#define LUA_TTHREAD 8
#define LUA_NUMTAGS 9
As Lua Document says that there only 8 basic types in Lua. However, there gets 10. I know LUA_TLIGHTUSERDATA and LUA_TUSERDATA could finnally represented as userdata, but what about LUA_TNONE? And what is the differences of none and nil?
As was already mentioned in the comments, none is used in the C API to check whether there is no value. Consider the following script:
function foo(arg)
print(arg)
end
foo(nil) --> nil
foo() --> nil
In Lua you can use select('#', ...) to get the number of parameters passed to foo and using C API you can check if the user supplied no argument at all (using lua_isnone). Consider the following small C library, which works like type, except that it can recognize, if no argument was given:
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
static int which_type(lua_State* L)
{
// at first, we start with the check for no argument
// if this is false, there has to be at least one argument
if(lua_isnone(L, 1))
{
puts("none");
}
// now iterate through all arguments and print their type
int n = lua_gettop(L);
for(int i = 1; i <= n; ++i)
{
if(lua_isboolean(L, i))
{
puts("boolean");
}
else if(lua_istable(L, i))
{
puts("table");
}
else if(lua_isstring(L, i) && !lua_isnumber(L, i))
{
puts("string");
}
else if(lua_isnumber(L, i))
{
puts("number");
}
else if(lua_isfunction(L, i))
{
puts("function");
}
else if(lua_isnil(L, i))
{
puts("nil");
}
else if(lua_isthread(L, i))
{
puts("thread");
}
else if(lua_isuserdata(L, i))
{
puts("userdata");
}
}
return 0;
}
static const struct luaL_Reg testclib_functions[] = {
{ "type", which_type },
{ NULL, NULL }
};
int luaopen_testclib(lua_State* L)
{
luaL_newlib(L, testclib_functions);
return 1;
}
Compile this with something like gcc -shared -fPIC -o testclib.so testclib.c. In lua, we now load the library and use the function type:
local p = require "testclib"
p.type(nil) --> nil
p.type({}) --> table
p.type("foo")) --> string
-- now call it without any arguments
p.type()) --> none, not nil
--type() -- error: bad argument #1 to 'type' (value expected)
Note that you can't get 'none' and someother type from one call (while it is possible to receive multiple types by using multiple arguments, e.g. p.type("foo", 42)). This is quite logical, since it would be a syntactic error to use something like this:
p.type(, 42) -- error
One use of this can be seen in the print function, where print(something) prints the value (even it is not valid, e.g. nil), where print() prints a newline.

Is there a language where we can mark value as result and then continue function/method body?

I was reading smalltalk tutorial and this idea came to my mind.
Assume we have some language and there instead of return we're marking some value as final return value and then we continue method, once method end is reached - no matter what else was called we're returning marked value unless something else specified manually like this:
Let's say ^^ is a operator that marks value for return if no explicit return is found till end of method
m1()
{
^^1;
some_other_code;
}
m2(par)
{
^^2;
if par == 1: return 1;
some code;
}
m3(par)
{
if par == 1: return 1;
else if par == 0: ^^0;
do some stuff;
if par < 0: return -1;
}
m1() should return 1
m2(0) should return 2
m2(1) should return 1
m3(0) should return 0
m3(1) should return 1
m3(-2) should return -1
This is a little similar to ruby's tap() but not the same
Pascal did that. The return value was set by assigning to the function name. Swift does something not quite the same: You can mark code anywhere that will be executed when the function exits. Your return statement exists, but only after other code written somewhere else gets performed.

What is the default return value of a Dart function?

The function below works even though I intentionally deleted 'return' command:
main() {
add(i) => i + 2; //I intentionally deleted 'return'
print(add(3)); //5
}
But, the function below doesn't work after I intentionally deleted the 'return' command.
main() {
makeAdder(num addBy) {
return (num i) {
addBy + i; //I intentionally deleted 'return'
};
}
var add2 = makeAdder(2);
print(add2(3) ); //expected 5, but null.
}
Edited to clarify my question.
The last sentence in the latter function above, add2(3) doesn't return a value(I expect 5) but just null returns.
My question is why 'addBy + i' of the latter function doesn't return contrary to the fact that 'add(i) => i + 2' of the first function returns 'i + 2'.
Edited again.
The answer is in the fact of '=>' being {return }, not just {}.
main() {
makeAdder(num addBy) => (num i) { return addBy + i; };
var add2 = makeAdder(2);
print(add2(3) ); // 5
}
Even the code below works as '=>' has 'return' command in it.
main() {
makeAdder(num addBy) => (num i) => addBy + i; ;
var add2 = makeAdder(2);
print(add2(3) ); //5
}
In Dart each function without an explicit return someValue; returns null;
The null object does not have a method 'call'.
makeAdder (add2) without return returns null and null(3) leads to the exception.
I would like to quote two important note here. It might help others:
Though Dart is Optionally typed ( meaning, specifying the return type of a function such as int or void is optional ), it always recommended to specify type wherever possible. In your code, as a sign of good programming practice, do mention the return type.
If your function does not return a value then specify void. If you omit the return type then it will by default return null.
All functions return a value. If no return value is specified, the statement return null; is implicitly appended to the function body.

Dart: Delegate the comparison operator of a derived class to comparison operator of base class

I want to overload the the comparison operator (==) in Dart to compare structures. Now I'm not sure how to do this for derived classes when I already have overloaded the comparison operator of the base class and want to reuse that.
Assuming I have a a base class like:
class Base
{
int _a;
String _b;
bool operator ==(Base other)
{
if (identical(other, this)) return true;
if (_a != other._a) return false;
if (_b != other._b) return false;
return true;
}
}
Then I declare I derived class that adds additional fields and also want to overload operator==. I only want to compare the additional fields in the derived class and delegate the comparison of Base fields to the Base class. In other programming languages I could do something like Base::operator==(other) or super.equals(other), but in Dart I can't figure out what's the best way to do it.
class Derived extends Base
{
int _c; // additional field
bool operator ==(Derived other)
{
if (identical(other, this)) return true;
if (_c != other._c) return false; // Comparison of new field
// The following approach gives the compiler error:
// Equality expression cannot be operand of another equality expression.
if (!(super.==(other))) return false;
// The following produces "Unnecessary cast" warnings
// It also only recursively calls the Derived operator
if ((this as Base) != (other as Base)) return false;
return true;
}
}
I guess what I could do is:
Compare all fields of base class also in the derived class: Is very error prone if base class get's changed and also doesn't work when base and derived are in different packages.
Declare an equals function with the same logic as currently operator == in it, call super.equals() to compare the base class and delegate all calls of operator== to the equals function. However it doesn't look too appealing to implement equals and operator ==.
So what's the best or recommended solution for this problem?
Ok, after some further experiments I figured it out on my own.
It's simply calling:
super==(other)
I tried it with super.operator==(other) and super.==(other) before, but didn't expect that the simple super==(other) is sufficient.
For the given example above the correct operator is:
bool operator ==(Derived other)
{
if (identical(other, this)) return true;
if (_c != other._c) return false;
if (!(super==(other))) return false;
return true;
}
Seems I am bumping a 5 year old question, but since now we have Dart 2...
the == operator can easily be defined inline.
class Base {
int a;
String b;
bool operator ==(other) => other is Base
&& other.a == a
&& other.b == b;
}
To reuse from a derived class, super == other still seems to be the way.
class Derived extends Base {
int c;
bool operator ==(other) => other is Derived
&& super == other
&& other.c == c;
}
Now this being said I discovered a major gotcha, the == operator that is called seems to be that of the left side of the comparision. That is Base == Derived will call Base's == comparision, while Derived == Base will call call Derived's == comparison (and subsequently Base's). This does seem reasonable, but had me scratching my head for a minute.
Ex:
main() {
Base b = new Base();
Derived d1 = new Derived();
Derived d2 = new Derived();
b.a = 6;
d1.a = 6;
d2.a = 6;
b.b = "Hi";
d1.b = "Hi";
d2.b = "Hi";
d1.c = 1;
d2.c = 1;
assert(d1 == d2); // pass
assert(b == d1); // PASS!!!
assert(d1 == b); // fail
}
(Note: I removed the private _'s from the fields for demonstration purposes.)
To avoid the issue where a base class incorrectly has equality with a child clase you can add the additional check for runtimeType as follows.
bool operator ==(other) => other is Base
&& this.runtimeType == other.runtimeType
&& other.a == a
&& other.b == b;

what would the purpose of the '!' be in a statement

I am reviewing scripts written by differenct coders and see many statement like:
((patindex('%,'+rtrim(ad.Dept)+',%', #vcP1Input) != 0) .
and I am wondering what the '!' is being used for.
! means not in this case.
So the != means not equal.
It means inequality.
Left side (patindex('%,'+rtrim(ad.Dept)+',%', #vcP1Input) is not equal to right side (0)
!= is the negation of ==
for example
if(obj == null)
{
// do stuff1
}
else
{
// do stuff2
}
is the same like
if(obj != null)
{
// do stuff2
}
else
{
// do stuff1
}
In TSQL, != means not equal to.
Your expression
((PATINDEX('%,' + RTRIM(ad.Dept)+',%', #vcP1Input) != 0)
is true if it can find the the trimmed value of ad.Dept in the string #vcP1Input, that is if PATINDEX returns anything else than 0.

Resources