### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

#### BrainChemistry

• Jr. Member
• Posts: 73
##### Nest assignment into if-statement
« on: January 09, 2016, 01:12:36 am »
Hi there,

I saw in C examples, expressions like "if (var = func()) = value {}" but I wonder if something like this is possible in Free Pascal?

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

#### BeniBela

• Hero Member
• Posts: 760
##### Re: Nest assignment into if-statement
« Reply #1 on: January 09, 2016, 01:28:52 am »
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

#### taazz

• Hero Member
• Posts: 5365
##### Re: Nest assignment into if-statement
« Reply #2 on: January 09, 2016, 01:30:59 am »
Hi there,

I saw in C examples, expressions like "if (var = func()) = value {}" but I wonder if something like this is possible in Free Pascal?

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

either
Code: Pascal  [Select][+][-]
1. ex_var := ex_func( ex_param )
2. if ( ex_var = 0) then
3. begin
4.   //whatever
5. end;
6.
or
Code: Pascal  [Select][+][-]
1.
2. if ( ex_func( ex_param ) = 0) then
3. begin
4.   //whatever
5. end;
6.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

#### BrainChemistry

• Jr. Member
• Posts: 73
##### Re: Nest assignment into if-statement
« Reply #3 on: January 09, 2016, 10:53:06 am »
Quote from: BeniBala
Pascal is an ugly language

Very rarely, but sometimes yes  .

The wrapping solution is nice but makes it more lines of code, that is what I'd like to prevent :-).

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

Yes, that is what I rather did before. The idea was to change it into one line.

Quote from: taazz
Code: Pascal  [Select][+][-]
1. if ( ex_func( ex_param ) = 0) then
2. begin
3.   //whatever
4. end;
I need to work with the return value afterwards, so this solution is neat but not suitable in my case.

Thanks you two for clearification  !

#### taazz

• Hero Member
• Posts: 5365
##### Re: Nest assignment into if-statement
« Reply #4 on: January 09, 2016, 11:16:55 am »
The wrapping solution is nice but makes it more lines of code, that is what I'd like to prevent :-).
Not really you just make sure you have a unit with overloaded functions for most of the basic types somewhere and use that when needed. Its a nice solution and not to different from your original showcase.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

#### User137

• Hero Member
• Posts: 1791
##### Re: Nest assignment into if-statement
« Reply #5 on: January 09, 2016, 04:07:03 pm »
There was another option:
Code: Pascal  [Select][+][-]
1. if ex_func(ex_param, ex_var) = 0 then
2. begin
3.   //whatever
4. end;
That just assumes ex_func() is made by you, and set the ex_var in it.
« Last Edit: January 09, 2016, 04:09:24 pm by User137 »

#### Edson

• Hero Member
• Posts: 1098
##### Re: Nest assignment into if-statement
« Reply #6 on: January 09, 2016, 05:43:51 pm »
Pascal syntax was made to be clear. Probably you will need more lines to implement, but it will be more readable.
Lazarus 2.0.6 - FPC 3.0.4 - x86_64-win64 on  Windows 8

#### wp

• Hero Member
• Posts: 7619
##### Re: Nest assignment into if-statement
« Reply #7 on: January 09, 2016, 06:31:24 pm »
more lines of code, that is what I'd like to prevent :-).
I don't understand what's the problem of writing one more line of code. Cramping "everything" into a single line of code will cost you hours of debugging months later when you'll have forgotten all the details.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### Leledumbo

• Hero Member
• Posts: 8273
• Programming + Glam Metal + Tae Kwon Do = Me
##### Re: Nest assignment into if-statement
« Reply #8 on: January 09, 2016, 06:42:00 pm »
People come from C based languages tend to be lazier, because debugging is the last chapter they learn

#### BeniBela

• Hero Member
• Posts: 760
##### Re: Nest assignment into if-statement
« Reply #9 on: January 09, 2016, 07:35:14 pm »

I don't understand what's the problem of writing one more line of code.

Every line of code is a liability.

The less code there is, the less time you will spend maintaining or debugging it in the long term.

You write one additional line here, and one there, and then they interact, and everything blows up

#### marcov

• Global Moderator
• Hero Member
• Posts: 8780
• FPC developer.
##### Re: Nest assignment into if-statement
« Reply #10 on: January 09, 2016, 09:23:25 pm »
Every line of code is a liability.

The less code there is, the less time you will spend maintaining or debugging it in the long term.

That only works if it is about orders of  magnitudes as a simplification, not on micro level.

One could as well say that every bit a language is larger, that carries an increased risk of programmer mistakes. (and e.g. Ada was heavily criticized for that)
« Last Edit: January 10, 2016, 12:30:40 am by marcov »

#### guest58172

• Guest
##### Re: Nest assignment into if-statement
« Reply #11 on: January 09, 2016, 10:54:40 pm »
Hi there,

I saw in C examples, expressions like "if (var = func()) = value {}" but I wonder if something like this is possible in Free Pascal?

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

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.

#### Abelisto

• Jr. Member
• Posts: 90
##### Re: Nest assignment into if-statement
« Reply #12 on: January 09, 2016, 11:09:13 pm »
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.

OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

#### BeniBela

• Hero Member
• Posts: 760
##### Re: Nest assignment into if-statement
« Reply #13 on: January 09, 2016, 11:19:01 pm »

#### Abelisto

• Jr. Member
• Posts: 90
##### Re: Nest assignment into if-statement
« Reply #14 on: January 09, 2016, 11:28:39 pm »
No, go there: http://forum.lazarus.freepascal.org/index.php/topic,31027.msg198146/topicseen.html#new

Here is not the topic you are looking for

No I am not mistaken It is about that the assignment operator in C language returns the value and in Pascal does not.
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk