+1Mistrel wrote:I would rather see support for the ternary operator.
Would really be cool for PureBasic to implement the ternary operator.

+1Mistrel wrote:I would rather see support for the ternary operator.
Lunasole wrote:Well, the IIF() is absolutely required as it can be seen from many posts about it. It is simple nice thing w/o noticeable side-effects and I wonder why not to add it not as operator, but as built-in function to some library.
This function is not as widely used as other logical, the little overhead produced by such function call looks definitely better than code trashed with a temp variables and lot of IF/ENDIF.
Code: Select all
Define S$ = IIF (expression, A, B) ; assuming IIF is implemented for return arguments of every base type. the compiler selects which implementation to use basing on type of A variable here (is returned if expression = true), B has to be the same type Define S2$ = IIF( Bool(IIF (expression, 100, 200) = 100), "100", "something else") ; something nested MessageRequester (IIF (expression, "str1", "str2"), "")
yes sir !Danilo wrote: Just a shortcut to If..Else..EndIf, with the additional advantage that it can be used as an expression.
Code: Select all
var.i = If x=2 { 12 } Else { 14 }
Code: Select all
TheFunction( 13, 14, If a=0 {„Yes“} Else {„No“} )
Code: Select all
text.s = "pieces" If n > 1 Else "piece"
Code: Select all
Macro TernS(expression,ifTrue,ifFalse)
TernaryS(Bool(expression),ifTrue,ifFalse)
EndMacro
Procedure.s TernaryS(condition, t.s, f.s)
If condition : ProcedureReturn t : Else : ProcedureReturn f : EndIf
EndProcedure
Debug "Ternary operator: "+TernS(a>b,"T","F")
Code: Select all
Macro IIF(res,cond,true,false)
!v_#res = cond ? true : false;
EndMacro
a = 6
b.s = "hello"
c.s = "byebye"
r.s = ""
a = 6
iif(r, v_a > 5, v_b ,v_c)
Debug r
a-1
iif(r, v_a > 5, v_b ,v_c)
Debug r
Code: Select all
Macro IIF(res,cond,true,false)
!v_#res = cond ? true : false;
EndMacro
a = 6
b.s = "hello"
c.s = "byebye"
r.s = ""
a = 6
iif(r, v_a > 5, v_b ,v_c)
Debug r
a-1
iif(r, v_a > 5, v_b ,v_c)
c="Ätsch"
Debug r
; hello
; Ätsch
Issue for some will be, one of the value points of PureBasic is to be lean and quick - like a calculator, you want its results to be prompt.
Code: Select all
; iff condition in calc
Define q.q,d.d,f.f,l.l,s.s, a=1
! __asm__("NOP");
! v_q=(v_a==1 ? 4711LL : 42LL);
! __asm__("NOP");
! v_d=(v_a==1 ? 47.11 : 42.0);
! __asm__("NOP");
! v_f=(v_a==1 ? 47.11 : 42.0);
! __asm__("NOP");
! v_l=(v_a==1 ? 47L : 42L );
! __asm__("NOP");
;!v_s=(v_a==1 ? "4711" : "42");
If a=1 : s="4711" : Else : s="42" : EndIf
! __asm__("NOP");
SetClipboardText(Str(q)+" "+StrD(d)+" "+StrF(f)+" "+Str(l)+" "+s)
; 4711 47.11 47.1100006104 47 4711
! __asm__("int3");
;
;X64
; 00000001400010FE | 90 | nop |
; 00000001400010FF | 48:8B05 62440000 | mov rax,qword ptr ds:[140005568] |
; 0000000140001106 | 48:83F8 01 | cmp rax,1 |
; 000000014000110A | 75 07 | jne iff.140001113 |
; 000000014000110C | B8 67120000 | mov eax,1267 |
; 0000000140001111 | EB 05 | jmp iff.140001118 |
; 0000000140001113 | B8 2A000000 | mov eax,2A | 2A:'*'
; 0000000140001118 | 48:8905 61440000 | mov qword ptr ds:[140005580],rax |
; 000000014000111F | 90 | nop |
; 0000000140001120 | 48:8B05 41440000 | mov rax,qword ptr ds:[140005568] |
; 0000000140001127 | 48:83F8 01 | cmp rax,1 |
; 000000014000112B | 75 0A | jne iff.140001137 |
; 000000014000112D | F2:0F1005 43200000 | movsd xmm0,qword ptr ds:[140003178] |
; 0000000140001135 | EB 08 | jmp iff.14000113F |
; 0000000140001137 | F2:0F1005 41200000 | movsd xmm0,qword ptr ds:[140003180] |
; 000000014000113F | F2:0F1105 29440000 | movsd qword ptr ds:[140005570],xmm0 |
; 0000000140001147 | 90 | nop |
; 0000000140001148 | 48:8B05 19440000 | mov rax,qword ptr ds:[140005568] |
; 000000014000114F | 48:83F8 01 | cmp rax,1 |
; 0000000140001153 | 75 0A | jne iff.14000115F |
; 0000000140001155 | F3:0F1005 2B200000 | movss xmm0,dword ptr ds:[140003188] | 0000000140003188:"¤p<B"
; 000000014000115D | EB 08 | jmp iff.140001167 |
; 000000014000115F | F3:0F1005 25200000 | movss xmm0,dword ptr ds:[14000318C] |
; 0000000140001167 | F3:0F1105 09440000 | movss dword ptr ds:[140005578],xmm0 | 0000000140005578:"¤p<B/"
; 000000014000116F | 90 | nop |
; 0000000140001170 | 48:8B05 F1430000 | mov rax,qword ptr ds:[140005568] |
; 0000000140001177 | 48:83F8 01 | cmp rax,1 |
; 000000014000117B | 75 07 | jne iff.140001184 |
; 000000014000117D | B8 2F000000 | mov eax,2F | 2F:'/'
; 0000000140001182 | EB 05 | jmp iff.140001189 |
; 0000000140001184 | B8 2A000000 | mov eax,2A | 2A:'*'
; 0000000140001189 | 8905 ED430000 | mov dword ptr ds:[14000557C],eax |
; 000000014000118F | 90 | nop |
; 0000000140001190 | 48:8B05 D1430000 | mov rax,qword ptr ds:[140005568] |
; 0000000140001197 | 48:83F8 01 | cmp rax,1 |
; 000000014000119B | 75 15 | jne iff.1400011B2 |
; 000000014000119D | 48:8D15 7C3E0000 | lea rdx,qword ptr ds:[140005020] | 0000000140005020:L"4711"
; 00000001400011A4 | 48:8D0D DD430000 | lea rcx,qword ptr ds:[140005588] | 0000000140005588:&L"4711"
; 00000001400011AB | E8 100B0000 | call iff.140001CC0 |
; 00000001400011B0 | EB 14 | jmp iff.1400011C6 |
; 00000001400011B2 | 90 | nop |
; 00000001400011B3 | 48:8D15 5E3E0000 | lea rdx,qword ptr ds:[140005018] |
; 00000001400011BA | 48:8D0D C7430000 | lea rcx,qword ptr ds:[140005588] | 0000000140005588:&L"4711"
; 00000001400011C1 | E8 FA0A0000 | call iff.140001CC0 |
; 00000001400011C6 | 90 | nop |
; // If a=1 : s="4711" : Else : s="42" : EndIf
; If (!((v_a==1))) { Goto no2; }
; SYS_FastAllocateStringFree4(&v_s,_S1);
; Goto endif1;
; no2:;
; SYS_FastAllocateStringFree4(&v_s,_S2);
; endif1: ;
; movq v_a(%rip), %rax
; cmpq $1, %rax
; jne .L17
; movl $4711, %eax
; jmp .L18
; .L17:
; movl $42, %eax
; .L18:
; movq %rax, v_q(%rip)
; /APP
; # 121 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movq v_a(%rip), %rax
; cmpq $1, %rax
; jne .L19
; movsd .LC2(%rip), %xmm0
; jmp .L20
; .L19:
; movsd .LC3(%rip), %xmm0
; .L20:
; movsd %xmm0, v_d(%rip)
; /APP
; # 125 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movq v_a(%rip), %rax
; cmpq $1, %rax
; jne .L21
; movss .LC4(%rip), %xmm0
; jmp .L22
; .L21:
; movss .LC5(%rip), %xmm0
; .L22:
; movss %xmm0, v_f(%rip)
; /APP
; # 129 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movq v_a(%rip), %rax
; cmpq $1, %rax
; jne .L23
; movl $47, %eax
; jmp .L24
; .L23:
; movl $42, %eax
; .L24:
; movl %eax, v_l(%rip)
; /APP
; # 133 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movq v_a(%rip), %rax
; cmpq $1, %rax
; jne .L29
; leaq _S1(%rip), %rax
; movq %rax, %rdx
; leaq v_s(%rip), %rax
; movq %rax, %rcx
; call SYS_FastAllocateStringFree4
; jmp .L27
; .L29:
; nop
; .L26:
; leaq _S2(%rip), %rax
; movq %rax, %rdx
; leaq v_s(%rip), %rax
; movq %rax, %rcx
; call SYS_FastAllocateStringFree4
; .L27:
;X86
; 0040108F | 90 | nop |
; 00401090 | 833D 30444000 01 | cmp dword ptr ds:[404430],1 |
; 00401097 | 0F84 A3010000 | je purebasic.401240 |
; 0040109D | B8 2A000000 | mov eax,2A | 2A:'*'
; 004010A2 | 31D2 | XOr edx,edx | edx:EntryPoint
; 004010A4 | A3 18444000 | mov dword ptr ds:[404418],eax |
; 004010A9 | 8915 1C444000 | mov dword ptr ds:[40441C],edx | edx:EntryPoint
; 004010AF | 90 | nop |
; 004010B0 | 833D 30444000 01 | cmp dword ptr ds:[404430],1 |
; 004010B7 | 0F84 93010000 | je purebasic.401250 |
; 004010BD | D905 08204000 | fld st(0),dword ptr ds:[402008] |
; 004010C3 | DD1D 28444000 | fstp qword ptr ds:[404428],st(0) |
; 004010C9 | 90 | nop |
; 004010CA | 833D 30444000 01 | cmp dword ptr ds:[404430],1 |
; 004010D1 | A1 08204000 | mov eax,dword ptr ds:[402008] |
; 004010D6 | 0F4405 0C204000 | cmove eax,dword ptr ds:[40200C] |
; 004010DD | A3 24444000 | mov dword ptr ds:[404424],eax |
; 004010E2 | 90 | nop |
; 004010E3 | 31C0 | XOr eax,eax |
; 004010E5 | 833D 30444000 01 | cmp dword ptr ds:[404430],1 |
; 004010EC | 0F94C0 | sete al |
; 004010EF | 8D4480 2A | lea eax,dword ptr ds:[eax+eax*4+2A] |
; 004010F3 | A3 20444000 | mov dword ptr ds:[404420],eax |
; 004010F8 | 90 | nop |
; 004010F9 | 833D 30444000 01 | cmp dword ptr ds:[404430],1 |
; 00401100 | 0F84 5A010000 | je purebasic.401260 |
; 00401106 | C74424 04 10404000 | mov dword ptr ss:[esp+4],purebasic.4040 |
; 0040110E | C70424 14444000 | mov dword ptr ss:[esp],purebasic.404414 |
; 00401115 | E8 A60A0000 | call purebasic.401BC0 |
; 0040111A | 83EC 08 | sub esp,8 |
; 0040111D | 90 | nop |
; /NO_APP
; movl _v_a, %eax
; cmpl $1, %eax
; jne L17
; movl $4711, %eax
; movl $0, %edx
; jmp L18
; L17:
; movl $42, %eax
; movl $0, %edx
; L18:
; movl %eax, _v_q
; movl %edx, _v_q+4
; /APP
; # 122 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movl _v_a, %eax
; cmpl $1, %eax
; jne L19
; fldl LC3
; jmp L20
; L19:
; fldl LC4
; L20:
; fstpl _v_d
; /APP
; # 126 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movl _v_a, %eax
; cmpl $1, %eax
; jne L21
; flds LC5
; jmp L22
; L21:
; flds LC6
; L22:
; fstps _v_f
; /APP
; # 130 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movl _v_a, %eax
; cmpl $1, %eax
; jne L23
; movl $47, %eax
; jmp L24
; L23:
; movl $42, %eax
; L24:
; movl %eax, _v_l
; /APP
; # 134 "purebasic.c" 1
; NOP
; # 0 "" 2
; /NO_APP
; movl _v_a, %eax
; cmpl $1, %eax
; jne L29
; movl $__S1, 4(%esp)
; movl $_v_s, (%esp)
; call _SYS_FastAllocateStringFree4@8
; subl $8, %esp
; jmp L27
; L29:
; nop
; L26:
; movl $__S2, 4(%esp)
; movl $_v_s, (%esp)
; call _SYS_FastAllocateStringFree4@8
; subl $8, %esp
; L27: