Recent

Author Topic: Nest assignment into if-statement  (Read 9541 times)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8783
  • FPC developer.
Re: Nest assignment into if-statement
« Reply #15 on: January 10, 2016, 12:31:40 am »
A statement is also an expression in C, even if it is only void.

BrainChemistry

  • Jr. Member
  • **
  • Posts: 73
Re: Nest assignment into if-statement
« Reply #16 on: January 10, 2016, 01:57:29 am »
Quote from: User137
There was another option: .... That just assumes ex_func() is made by you, and set the ex_var in it.

In the real case, the function isn't made by me.

Quote from: Edson
Pascal syntax was made to be clear. Probably you will need more lines to implement, but it will be more readable.  :)

I agree, and I like it. Compared to C possibilities to cryptify source code, the one-liner here is obvious to the reader and just convenient.

Quote from: wp
Cramping "everything" into a single line of code will cost you hours of debugging months later when you'll have forgotten all the details.

It isn't as if the line I asked for is so cryptic to not get the idea behind this. (I know what you mean when you're talking about C though.)

The problem is that in the Pascal construct if expression then statement, expression must be something that can be evaluated to boolean. The feature you speak about for example is more useful with languages that makes shortcuts in their if expression:

Code: D  [Select][+][-]
  1. if (auto vt = "key" in AA) {}
<=>
Code: D  [Select][+][-]
  1. ValueType* vt = "key" in AA;
  2. if (vt != null){}
or
Code: D  [Select][+][-]
  1. if (size_t len = array.length) {}
<=>
Code: D  [Select][+][-]
  1. size_t len = array.length;
  2. if (len != 0) {}

Because here the if expression allows to pass a pointer or an integral type (and boolean types of course), then the feature your speak about is yet more interesting.

I'm not sure if I get you right. The examples you constructed do not make it obvious to me why in Pascal this shouldn't be allowed. Unless this "feature" is just not supported in Pascal, meaning, assignments in the expression area are not allowed. - To my knowledge, I could construct similar code for pointer and integer types (and pass them). Hence, not sure about this remark.

Quote from: Abelisto
It is about that the assignment operator in C language returns the value and in Pascal does not.

That makes sense, since

Code: Pascal  [Select][+][-]
  1. if ( ex_func( ex_param ) = 0) then
  2. begin
  3.   //whatever
  4. end;

works.

I would have expected the assignment operator would return the value to the variable which then would be evaluated by the if-statement. Then both expressions (C and Pascal from OP) should work the same. - If the assignment operator in C assigns AND returns (like a function) but the Pascal assignment operator just assigns, the if-statement at the same time does not evaluate the variable value but the return value, I see that it never could work.

Thanks for the clarification!

fic

  • New Member
  • *
  • Posts: 14
Re: Nest assignment into if-statement
« Reply #17 on: January 10, 2016, 10:58:17 am »
Lets discuss the simplified example that directly related to the topic:

Code: C  [Select][+][-]
  1. int a, b, c;
  2. a = b = c = 1;
  3.  

 ;)

To be honest? This declaration is the one thing I really miss in Pascal, and that's the only thing!
Sure, one can write code in a few lines in C++, where a Pascal-programmer probably needs 10 lines or more to achieve the same result.
Lately, I've read lots of things people want to change in FPC/Lazarus, but this is a Pascal-forum.
Although I like this discussion. Pascal is and will hopefully stay a pgm-language in the way it was developed.
Sure, every language has its faults (and/or goods), but people here try to accomplisch something...
If you guys want to change that? Well, create your own Lazarus or C-Lazarus, or Java-Lazarus, or Ada-Lazarus... And please... Make it work like you all want!


Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Nest assignment into if-statement
« Reply #18 on: February 09, 2016, 12:33:14 pm »
You can only do it like this:

Code: [Select]
function assignwrap(out assignee: integer; const data: integer): integer;
begin
  result := data;
  assignee := data;
end;

if assignwrap(ex_var, ex_func(ex_param)) = 0 then ...



Pascal is an ugly language

Nope. write inline and look at the asm output:
Code: Pascal  [Select][+][-]
  1. [code]
  2. function assignwrap(out assignee: integer; const data: integer): integer;inline;
  3. begin
  4.   result := data;
  5.   assignee := data;
  6. end;

Now, go and play with your toy.

There are many more ways to do that of course, but I'd thought it appropriate to stick to inline.

« Last Edit: February 09, 2016, 12:35:29 pm by Thaddy »

 

TinyPortal © 2005-2018