You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/17 21:57:59 UTC

[GitHub] [arrow-cookbook] nirandaperera commented on a change in pull request #166: [C++] Add TypeVisitor example

nirandaperera commented on a change in pull request #166:
URL: https://github.com/apache/arrow-cookbook/pull/166#discussion_r829544343



##########
File path: cpp/source/basic.rst
##########
@@ -48,3 +48,29 @@ boilerplate for you.  It will run the contained expression and check the resulti
 .. recipe:: ../code/basic_arrow.cc ReturnNotOk
   :caption: Using ARROW_RETURN_NOT_OK to check the status
   :dedent: 2
+
+
+Using the Visitor Pattern
+=========================
+
+Arrow classes like DataType, Scalar, and Array have specialized subclasses for
+each Arrow type. In order to work with them, use the visitor pattern. These 
+types provide an Accept method and have associated Visitor classes.
+
+As an example, below we implement a :cpp:class:`arrow::TypeVisitor` that counts
+the number of  primitive and nested types.
+
+To implement a TypeVisitor we have to implement a Visit method for every possible
+DataType we wish to handle. Fortunately, we can often use templates and type
+traits to make this less verbose.
+
+.. literalinclude:: ../code/basic_arrow.cc
+   :language: cpp
+   :linenos:
+   :start-at: class TypeCountVisitor
+   :end-at: };  // TypeCountVisitor
+   :caption: TypeVisitor that counts nested and non-nested types

Review comment:
       IMO https://github.com/apache/arrow/pull/11886 was a really good example of a `TypeVisitor` :-) 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org