Skip to content

Commit

Permalink
FEAT: making UNSET! as a conditionally "TRUTHY"
Browse files Browse the repository at this point in the history
related issues:
metaeducation/rebol-issues#564
metaeducation/rebol-issues#850

It reverts changes discussed in above issues as I consider this behavior to be more useful and it is compatible with Rebol2 and Red.

My main reason is to be able use PRINT inside ALL blocks without need to worry if these would affect the evaluation.

Like in this case:
```
	if all [
		not headers
		any [
			all [
				d1: find conn/data crlfbin
				d2: find/tail d1 crlf2bin
				print "server using standard content separator of #{0D0A0D0A}"
			]
			all [
				d1: find conn/data #{0A}
				d2: find/tail d1 #{0A0A}
				print "server using malformed line separator of #{0A0A}"
			]
		]
	][ ... ]

```
  • Loading branch information
Oldes committed Oct 7, 2018
1 parent c5cfb69 commit cf740c4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/boot/natives.r
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ do: native [

either: native [
{If TRUE condition return first arg, else second; evaluate blocks by default.}
condition
condition [any-type!]
true-branch
false-branch
/only "Suppress evaluation of block args."
Expand Down Expand Up @@ -188,7 +188,7 @@ halt: native [

if: native [
{If TRUE condition, return arg; evaluate blocks by default.}
condition
condition [any-type!]
true-branch
/only "Return block arg instead of evaluating it."
]
Expand Down Expand Up @@ -307,7 +307,7 @@ try: native [

unless: native [
{If FALSE condition, return arg; evaluate blocks by default.}
condition
condition [any-type!]
false-branch
/only "Return block arg instead of evaluating it."
]
Expand Down Expand Up @@ -818,7 +818,7 @@ log-e: native [

not: native [
{Returns the logic complement.}
value {(Only FALSE and NONE return TRUE)}
value [any-type!] {(Only FALSE and NONE return TRUE)}
]

square-root: native [
Expand Down
2 changes: 1 addition & 1 deletion src/core/n-control.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ enum {
while (index < SERIES_TAIL(block)) {
index = Do_Next(block, index, 0); // stack volatile
ds = DS_POP; // volatile stack reference
if (!IS_FALSE(ds) && !IS_UNSET(ds)) return R_TOS1;
if (!IS_FALSE(ds)) return R_TOS1;
}
return R_NONE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mezz/base-defs.r
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ scalar?: func [

true?: func [
"Returns true if an expression can be used as true."
val ; Note: No [any-type!] - we want unset! to fail.
val [any-type!] ;- we want unset! not to fail.
] [not not :val]

quote: func [
Expand Down
1 change: 1 addition & 0 deletions src/tests/run-tests.r3
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ wrap load %units/dh-test.r3
wrap load %units/port-test.r3
wrap load %units/checksum-test.r3
wrap load %units/enum-test.r3
wrap load %units/conditional-test.r3

wrap load %units/crash-test.r3

Expand Down
32 changes: 32 additions & 0 deletions src/tests/units/conditional-test.r3
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Rebol [
Title: "Rebol conditional test script"
Author: "Olds"
File: %conditional-test.r3
Tabs: 4
Needs: [%../quick-test-module.r3]
]

~~~start-file~~~ "Conditional"

===start-group=== "Dealing with unset! value in conditions"
--test-- "any"
--assert unset? any [() 2]
--assert true? any [() 2]
--test-- "all"
--assert true? all []
--assert true? all [()]
--assert 3 = all [() 1 2 3]
--assert unset? all [1 ()]
--assert 1 = if all [1 ()][1]
--assert 1 = either all [()][1][2]
--test-- "any and all"
if any [
all [false x: 1 ()]
all [true x: 2 ()]
][ x: 2 * x]
--assert x = 4


===end-group===

~~~end-file~~~

0 comments on commit cf740c4

Please sign in to comment.