{"id":3035,"date":"2023-03-05T17:36:34","date_gmt":"2023-03-05T12:06:34","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3035"},"modified":"2023-03-06T05:09:47","modified_gmt":"2023-03-05T23:39:47","slug":"julia-defining-functions-dynamically","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/","title":{"rendered":"Julia: Defining Functions Dynamically"},"content":{"rendered":"<p>One of the widely discussed features of <em><strong><a href=\"https:\/\/julialang.org\/\" target=\"_blank\" rel=\"noopener\">Julia<\/a><\/strong><\/em> is its support for <em><strong>metaprogramming<\/strong><\/em>. This feature makes it possible to generate and inspect the code at runtime. In essence, metaprogramming blurs the distinction between code and data. When used carefully, it can contribute to good code.<\/p>\n<p>Of course, <em><strong>Julia is<\/strong><\/em> not the only or the first language to support Metaprogramming. Many other languages such as <em><strong>Lisp<\/strong><\/em>, <em><strong>Clojure<\/strong><\/em>, <em><strong>Ruby<\/strong><\/em>, <em><strong>Elixir<\/strong><\/em>, and <em><strong>Python<\/strong><\/em> also have support for metaprogramming.<\/p>\n<p>In today\u2019s article, I would like to show how we can define functions at <em><strong>runtime<\/strong><\/em> in <em><strong>Julia<\/strong><\/em>. This technique applies to generating any code, although my focus is on defining functions.<\/p>\n<h3>1) Generating using Strings<\/h3>\n<p>This is the simplest way to generate runtime code. We use the <em><strong>\u201cMeta.parse()\u201d<\/strong><\/em> function for this. The following example shows how it is done:<\/p>\n<figure id=\"attachment_3036\" aria-describedby=\"caption-attachment-3036\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3036\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/example1-30\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1.jpg\" data-orig-size=\"744,484\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678006662&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Defining A Function Dynamically\" data-image-description=\"&lt;p&gt;Defining A Function Dynamically&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Defining A Function Dynamically&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1.jpg\" class=\"wp-image-3036\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1.jpg?resize=550%2C358&#038;ssl=1\" alt=\"Defining A Function Dynamically\" width=\"550\" height=\"358\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1.jpg?resize=300%2C195&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1.jpg?resize=140%2C90&amp;ssl=1 140w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1.jpg?w=744&amp;ssl=1 744w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3036\" class=\"wp-caption-text\"><strong>Defining A Function Dynamically<\/strong><\/figcaption><\/figure>\n<p>The function <em><strong>\u201cToFunction()\u201d<\/strong><\/em> takes a <em><strong>String<\/strong><\/em> argument and converts that to a <em><strong>Julia<\/strong><\/em> function. Cell[2] shows how this works. As cell[4] shows, the function can be invoked using its defined name.<\/p>\n<p>Here is another example that defines an <em><strong>\u201canonymous\u201d<\/strong><\/em> function. The behaviour is the same.<\/p>\n<figure id=\"attachment_3037\" aria-describedby=\"caption-attachment-3037\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3037\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/example2-26\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example2.jpg\" data-orig-size=\"569,213\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678006704&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Defining Anonymous Function Dynamically\" data-image-description=\"&lt;p&gt;Defining Anonymous Function Dynamically&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Defining Anonymous Function Dynamically&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example2.jpg\" class=\"wp-image-3037\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example2.jpg?resize=500%2C187&#038;ssl=1\" alt=\"Defining Anonymous Function Dynamically\" width=\"500\" height=\"187\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example2.jpg?resize=300%2C112&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example2.jpg?w=569&amp;ssl=1 569w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3037\" class=\"wp-caption-text\"><strong>Defining Anonymous Function Dynamically<\/strong><\/figcaption><\/figure>\n<p>Some programmers might prefer to define <em><strong>\u201cToFunction()\u201d<\/strong> <\/em>as a <em><strong>\u201cmacro\u201d<\/strong><\/em> instead of as a regular function. This permits the function synthesis to happen in two stages: the <em><strong>AST<\/strong><\/em> is generated at <em><strong>\u201ccompile time\u201d<\/strong><\/em> and is turned into a function at runtime.<\/p>\n<figure id=\"attachment_3038\" aria-describedby=\"caption-attachment-3038\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example3.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3038\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/example3-22\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example3.jpg\" data-orig-size=\"735,486\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678006763&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Defining A Macro Instead of Function\" data-image-description=\"&lt;p&gt;Defining A Macro Instead of Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Defining A Macro Instead of Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example3.jpg\" class=\"wp-image-3038\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example3.jpg?resize=550%2C364&#038;ssl=1\" alt=\"Defining A Macro Instead of Function\" width=\"550\" height=\"364\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example3.jpg?resize=300%2C198&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example3.jpg?w=735&amp;ssl=1 735w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3038\" class=\"wp-caption-text\"><strong>Defining A Macro Instead of Function<\/strong><\/figcaption><\/figure>\n<p>We can tweak our function slightly so that it accepts multiple arguments:<\/p>\n<figure id=\"attachment_3039\" aria-describedby=\"caption-attachment-3039\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3039\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/example4-16\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example4.jpg\" data-orig-size=\"881,371\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678007031&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Refining the Macro\" data-image-description=\"&lt;p&gt;Refining the Macro&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Refining the Macro&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example4.jpg\" class=\"wp-image-3039\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example4.jpg?resize=550%2C232&#038;ssl=1\" alt=\"Refining the Macro\" width=\"550\" height=\"232\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example4.jpg?resize=300%2C126&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example4.jpg?resize=768%2C323&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example4.jpg?w=881&amp;ssl=1 881w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3039\" class=\"wp-caption-text\"><strong>Refining the Macro<\/strong><\/figcaption><\/figure>\n<p>Needless to say, the supplied <em><strong>String<\/strong><\/em> argument must represent a valid function definition. If not, we will get an error:<\/p>\n<figure id=\"attachment_3040\" aria-describedby=\"caption-attachment-3040\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Error.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3040\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/error\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Error.jpg\" data-orig-size=\"735,408\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678008498&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Function Definition Error\" data-image-description=\"&lt;p&gt;Function Definition Error&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Function Definition Error&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Error.jpg\" class=\"wp-image-3040\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Error.jpg?resize=550%2C305&#038;ssl=1\" alt=\"Function Definition Error\" width=\"550\" height=\"305\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Error.jpg?resize=300%2C167&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Error.jpg?w=735&amp;ssl=1 735w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3040\" class=\"wp-caption-text\"><strong>Function Definition Error<\/strong><\/figcaption><\/figure>\n<h3>2) Generating using AST<\/h3>\n<p>Instead of using a <em><strong>String<\/strong><\/em> representation of the function to be defined, it is possible to work directly at the level of <em><strong>AST<\/strong><\/em>. This is essentially how <em><strong>\u201cmacros\u201d<\/strong><\/em> work. In the following example, the macro <em><strong>\u201cMakeFunction()\u201d<\/strong><\/em> takes an arbitrary <em><strong>\u201cExpr\u201d<\/strong><\/em> object as its argument and embeds it inside the body of the function to be synthesized.<\/p>\n<figure id=\"attachment_3041\" aria-describedby=\"caption-attachment-3041\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example5.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3041\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/example5-13\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example5.jpg\" data-orig-size=\"686,477\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678007938&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Synthesis Using AST\" data-image-description=\"&lt;p&gt;Synthesis Using AST&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Synthesis Using AST&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example5.jpg\" class=\"wp-image-3041\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example5.jpg?resize=550%2C382&#038;ssl=1\" alt=\"Synthesis Using AST\" width=\"550\" height=\"382\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example5.jpg?resize=300%2C209&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example5.jpg?w=686&amp;ssl=1 686w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3041\" class=\"wp-caption-text\"><strong>Synthesis Using AST<\/strong><\/figcaption><\/figure>\n<p>Cell[21] shows how this macro is invoked. Note that the generated function is <em><strong>\u201canonymous\u201d<\/strong><\/em> and hence must be called using the returned function object. Cells[22] and [23] show that the function works as expected.<\/p>\n<p>Here is another example that passes an expression involving a variable in the current scope:<\/p>\n<figure id=\"attachment_3042\" aria-describedby=\"caption-attachment-3042\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example6.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3042\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/example6-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example6.jpg\" data-orig-size=\"586,430\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678008359&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using a Variable in the Expression\" data-image-description=\"&lt;p&gt;Using a Variable in the Expression&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using a Variable in the Expression&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example6.jpg\" class=\"wp-image-3042\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example6.jpg?resize=500%2C367&#038;ssl=1\" alt=\"Using a Variable in the Expression\" width=\"500\" height=\"367\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example6.jpg?resize=300%2C220&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example6.jpg?w=586&amp;ssl=1 586w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3042\" class=\"wp-caption-text\"><strong>Using a Variable in the Expression<\/strong><\/figcaption><\/figure>\n<p>As cells[31] and [32] show, this also works correctly.<\/p>\n<p>There is a useful function called <a href=\"https:\/\/docs.julialang.org\/en\/v1\/base\/base\/#Base.methods\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cmethods()\u201d<\/strong><\/em><\/a>\u00a0 that returns the method table for any given function. We can use it in our case to learn about the <em><strong>\u201cdynamic\u201d<\/strong><\/em> method that we synthesized:<\/p>\n<figure id=\"attachment_3043\" aria-describedby=\"caption-attachment-3043\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/HowToFindMethods.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3043\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/howtofindmethods\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/HowToFindMethods.jpg\" data-orig-size=\"642,158\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1678025006&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The Methods() Function\" data-image-description=\"&lt;p&gt;The Methods() Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Methods() Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/HowToFindMethods.jpg\" class=\"wp-image-3043\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/HowToFindMethods.jpg?resize=450%2C111&#038;ssl=1\" alt=\"The Methods() Function\" width=\"450\" height=\"111\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/HowToFindMethods.jpg?resize=300%2C74&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/HowToFindMethods.jpg?w=642&amp;ssl=1 642w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3043\" class=\"wp-caption-text\"><strong>The Methods() Function<\/strong><\/figcaption><\/figure>\n<p>The examples discussed above only \u201cscratch the surface\u201d as far as metaprogramming in <em><strong>Julia<\/strong><\/em> is concerned. I encourage you to visit the <a href=\"https:\/\/docs.julialang.org\/en\/v1\/manual\/metaprogramming\/\" target=\"_blank\" rel=\"noopener\">documentation<\/a> to learn more about this topic.<\/p>\n<p>You can <a href=\"http:\/\/www.rangakrish.com\/downloads\/DynamicDefinition.ipynb\" target=\"_blank\" rel=\"noopener\"><em><strong>download<\/strong><\/em><\/a> the <em><strong>JupyterLab<\/strong><\/em> notebook used in this article.<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the widely discussed features of Julia is its support for metaprogramming. This feature makes it possible to generate and inspect the code at runtime. In essence, metaprogramming blurs the distinction between code and data. When used carefully, it can contribute to good code. Of course, Julia is not the only or the first [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[63,17],"tags":[64,353],"class_list":["post-3035","post","type-post","status-publish","format-standard","hentry","category-julia","category-programming","tag-julia","tag-metaprogramming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-MX","jetpack-related-posts":[{"id":278,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/07\/07\/multimethods-in-julia\/","url_meta":{"origin":3035,"position":0},"title":"Multimethods in Julia","author":"admin","date":"July 7, 2016","format":false,"excerpt":"I got interested in Julia programming language quite recently, primarily because of a project involving image processing and machine learning. The language is still evolving, but already has a rich set of features and a good collection of external libraries\u00a0covering many areas. One of the highlights of the language is\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Multimethods Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3017,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/02\/16\/calling-external-dll-functions-from-julia\/","url_meta":{"origin":3035,"position":1},"title":"Calling External DLL Functions from Julia","author":"admin","date":"February 16, 2023","format":false,"excerpt":"Julia supports calling external functions, especially those written in \u201cC\u201d language. As Julia documentation says, such calls do not involve any \u201cboilerplate\u201d code and hence are efficient. In today\u2019s article, I am going to show how to call functions defined in a DLL (Windows 64 bit). Instead of using direct\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"The \"lispEval\" Function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/02\/code-300x117.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3675,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/04\/24\/interfaces-without-inheritance-comparing-c-and-common-lisp\/","url_meta":{"origin":3035,"position":2},"title":"Interfaces Without Inheritance: Comparing C++ and Common Lisp","author":"admin","date":"April 24, 2025","format":false,"excerpt":"Clean interface design is a crucial aspect of software engineering since it enables code flexibility, reuse, and maintainability. Developers who prefer an object-oriented approach typically rely on inheritance to define the interface and thus establish type relationships. While this can lead to a good design if approached carefully, detractors of\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Rectangle and Circle Defined","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/04\/cpp2-251x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2935,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/07\/why-learn-c\/","url_meta":{"origin":3035,"position":3},"title":"Why Learn C++","author":"admin","date":"November 7, 2022","format":false,"excerpt":"This is the third and the last article in the series \u201cWhy Learn Language-XYZ?\u201d. I talked about Prolog and Lisp earlier. The present article is on C++. Once you have gained reasonable proficiency with Prolog and Lisp, you are ready to learn C++! In my view, C++ is a complex\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2947,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/24\/using-julia-from-mathematica\/","url_meta":{"origin":3035,"position":4},"title":"Using Julia from Mathematica","author":"admin","date":"November 24, 2022","format":false,"excerpt":"In an earlier article, I had shown how it is possible to interact with Mathematica from Julia. In today\u2019s article, I will share the details of how to interact with Julia from within Mathematica. Why would somebody want to execute Julia code inside Mathematica? Although Mathematica is a great symbolic\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Installing Julia","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/11\/Julia-Shell.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4012,"url":"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/","url_meta":{"origin":3035,"position":5},"title":"Common Lisp Metaobject Protocol: Classes are Just Objects!","author":"admin","date":"January 1, 2026","format":false,"excerpt":"In today\u2019s popular languages such as C++, Java, Golang, Rust, Python, etc., classes are fixed constructs defined by the language. They have a definite syntax that can\u2019t be changed while programming. We are all used to this of course. But what makes Common Lisp stand out is that in that\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"A Simple Class","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1-300x42.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=3035"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3035\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}