]> gitweb.factorcode.org Git - factor.git/commitdiff
alien.syntax: clarify that we can dispatch off ENUM: members
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 17 Sep 2022 19:08:36 +0000 (15:08 -0400)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 17 Sep 2022 19:09:06 +0000 (15:09 -0400)
basis/alien/syntax/syntax-docs.factor

index 522d11a5031a36bcae1b3fe4a8649adee2da1c48..8a72fe2bd69333e4d16e525fc5a801b08da8c87a 100644 (file)
@@ -67,18 +67,34 @@ HELP: TYPEDEF:
 HELP: ENUM:
 { $syntax "ENUM: type words... ;" "ENUM: type < base-type words..." }
 { $values { "type" { $maybe "a name to typedef to int" } } { "words" "a sequence of word names" } }
 HELP: ENUM:
 { $syntax "ENUM: type words... ;" "ENUM: type < base-type words..." }
 { $values { "type" { $maybe "a name to typedef to int" } } { "words" "a sequence of word names" } }
-{ $description "Creates a c-type that boxes and unboxes integer values to symbols. A symbol is defined for each member word. The base c-type can optionally be specified and defaults to " { $link int } ". A constructor word " { $snippet "<type>" } " is defined for converting from integers to singletons. The generic word " { $link enum>number } " converts from singletons to integers. Enum-typed values are automatically prettyprinted as their singleton words. Unrecognizing enum numbers are kept as numbers." }
+{ $description "Creates a c-type that boxes and unboxes integer values to symbols. A singleton is defined for each member word which allows generic dispatch on the enum's members. The base c-type can optionally be specified and defaults to " { $link int } ". A constructor word " { $snippet "<type>" } " is defined for converting from integers to singletons. The generic word " { $link enum>number } " converts from singletons to integers. Enum-typed values are automatically prettyprinted as their singleton words. Unrecognizing enum numbers are kept as numbers." }
 { $examples
     "Here is an example enumeration definition:"
     { $code "ENUM: color_t red { green 3 } blue ;" }
 { $examples
     "Here is an example enumeration definition:"
     { $code "ENUM: color_t red { green 3 } blue ;" }
+    $nl
     "The following expression returns true:"
     { $code "3 <color_t> [ green = ] [ enum>number 3 = ] bi and" }
     "The following expression returns true:"
     { $code "3 <color_t> [ green = ] [ enum>number 3 = ] bi and" }
-
+    $nl
     "Here is a version where the C-type takes a single byte:"
     "Here is a version where the C-type takes a single byte:"
-    { $code "ENUM: tv_peripherals_1 < uchar\n{ appletv 1 } { chromecast 2 } { roku 4 } ;" }
-
+    { $code "ENUM: tv_peripherals_1 < uchar"
+            "{ appletv 1 } { chromecast 2 } { roku 4 } ;"
+    }
+    $nl
     "The same as above but four bytes instead of one:"
     "The same as above but four bytes instead of one:"
-    { $code "ENUM: tv_peripherals_4 < uint\n{ appletv 1 } { chromecast 2 } { roku 4 } ;" }
+    { $code "ENUM: tv_peripherals_4 < uint"
+            "{ appletv 1 } { chromecast 2 } { roku 4 } ;"
+    }
+    $nl
+    "We can define a generic and dispatch on it:"
+    { $code "ENUM: tv_peripherals_4 < uint"
+            "{ appletv 1 } { chromecast 2 } { roku 4 } ;"
+            ""
+            "GENERIC: watch-device ( device -- )"
+            "M: appletv watch-device drop \"watching appletv\" print ;"
+            "M: chromecast watch-device drop \"watching chromecast\" print ;"
+            ""
+            "appletv watch-device"
+    }
 } ;
 
 HELP: C-TYPE:
 } ;
 
 HELP: C-TYPE: