# metaprogramming

Published articles for metaprogramming.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Detecting field names with C++ metaprogramming

DevFeed: [Detecting field names with C++ metaprogramming](<https://devfeed.tech/articles/detecting-field-names-with-c-metaprogramming-40505.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/shortform/2024-06-25-1534/>)

Published: 2024-06-25T22:34:59Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Code](<https://devfeed.tech/topics/code.md>), [Template](<https://devfeed.tech/topics/template.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code](<https://devfeed.tech/tags/code.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [shortform](<https://devfeed.tech/tags/shortform.md>), [template](<https://devfeed.tech/tags/template.md>), [templates](<https://devfeed.tech/tags/templates.md>)

### AI overview

This short note explains how C++11 templates can detect whether a struct has a field with a specific name and type, then use the result for compile-time branching. It presents a SFINAE-based implementation using HasStaticSize and type traits.

### Source excerpt

A quick note: you can use C++11 templates to detect struct fields by name and type, and statically branch on them. I first heard of this solution from breeze1990. Say I want to detect if a struct has a field size of type int. Create two template instantiations of the same name, here HasStaticSize that defaults to false. #include <type_traits> template <typename T, typename = void> struct HasStaticSize : std::false_type {}; template <typename T> struct HasStaticSize< T, typename std::enable_if< std::is_same<int, std::decay_t<decltype(T::size)>>::value, void>::type> : std::true_type {}; The latter is only resolved if T::size is declared as int, or more specifically, something that "decays" to int.

## Calling Haskell from Swift

DevFeed: [Calling Haskell from Swift](<https://devfeed.tech/articles/calling-haskell-from-swift-27915.md>)

Original publisher: [Read original article](<http://alt-romes.github.io/posts/2024-04-02-calling-haskell-from-swift.html>)

Published: 2024-04-02T00:00:00Z

Content type: tutorial

Language: en

Sources: [Romes' Musings](<https://devfeed.tech/sources/romes-musings.md>)

Topics: [Haskell](<https://devfeed.tech/topics/haskell.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [interoperability](<https://devfeed.tech/topics/interoperability.md>), [data type](<https://devfeed.tech/topics/data-type.md>), [iOS](<https://devfeed.tech/topics/ios.md>), [macOS](<https://devfeed.tech/topics/macos.md>), [Xcode](<https://devfeed.tech/topics/xcode.md>), [SwiftUI](<https://devfeed.tech/topics/swiftui.md>)

Tags: [data-type](<https://devfeed.tech/tags/data-type.md>), [haskell](<https://devfeed.tech/tags/haskell.md>), [interoperability](<https://devfeed.tech/tags/interoperability.md>), [ios](<https://devfeed.tech/tags/ios.md>), [macos](<https://devfeed.tech/tags/macos.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [swift](<https://devfeed.tech/tags/swift.md>), [swiftui](<https://devfeed.tech/tags/swiftui.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>), [xcode](<https://devfeed.tech/tags/xcode.md>)

### AI overview

This tutorial explains how to call non-trivial Haskell functions from Swift in native macOS and iOS applications. It uses foreign function exports, argument and result marshaling, serialization of user-defined data types, and Swift interoperability so Haskell functions can be exposed through Swift structs, classes, and idiomatic interfaces.

### Source excerpt

Contents 1 Introduction 2 Marshaling Inputs and Outputs 2.1 Haskell's Perspective 2.2 Swift's Perspective 3 Metaprogramming at the boundaries 3.1 Haskell's perspective 3.2 Swift's perspective 4 Remarks This is the second installment of the in-depth series of blog-posts on developing native macOS and iOS applications using both Haskell and Swift/SwiftUI. This post covers how to call (non-trivial) Haskell functions from Swift by using a foreign function calling-convention strategy similar to that described by Calling Purgatory from Heaven: Binding to Rust in Haskell that requires argument and result marshaling. You may find the other blog posts in this series interesting: Creating a macOS app with Haskell and Swift The series of blog posts is further accompanied by a github repository where each commit matches a step of this tutorial. If in doubt regarding any step, check the matching commit to make it clearer. This write-up has been cross-posted to Well-Typed's Blog. 1 Introduction We'll pick up from where the last post ended - we have set up an XCode project that includes our headers generated from Haskell modules with foreign exports and linking against the foreign library declared in the cabal file. We have already been able to call a very simple Haskell function on integers from Swift via Haskell's C foreign export feature and Swift's C interoperability. This part concerns itself with calling idiomatic Haskell functions, which typically involve user-defined datatypes as inputs and outputs, from Swift. Moreover, these functions should be made available to Swift transparently, such that Swift calls them as it does other idiomatic functions, with user defined structs and classes. For the running example, the following not-very-interesting function will suffice to showcase the method we will use to expose this function from Haskell to Swift, which easily scales to other complex data types and functions. data User = User { name :: String , age :: Int } birthday :: Use

## How Do You Know What ActiveRecord Table Has a user\_id Attribute?

DevFeed: [How Do You Know What ActiveRecord Table Has a user\_id Attribute?](<https://devfeed.tech/articles/how-do-you-know-what-activerecord-table-has-a-user-id-attribute-28248.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2019/12/28/how-do-you-know-what-activerecord-table-has-a-user-id-attribute.html>)

Author: Fuzzygroup

Published: 2019-12-28T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Development](<https://devfeed.tech/topics/development.md>), [data-modeling](<https://devfeed.tech/topics/data-modeling.md>), [business logic](<https://devfeed.tech/topics/business-logic.md>)

Tags: [active-record](<https://devfeed.tech/tags/active-record.md>), [activerecord](<https://devfeed.tech/tags/activerecord.md>), [business-logic](<https://devfeed.tech/tags/business-logic.md>), [class](<https://devfeed.tech/tags/class.md>), [classes](<https://devfeed.tech/tags/classes.md>), [console](<https://devfeed.tech/tags/console.md>), [development](<https://devfeed.tech/tags/development.md>), [id](<https://devfeed.tech/tags/id.md>), [jumpstart](<https://devfeed.tech/tags/jumpstart.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [migration](<https://devfeed.tech/tags/migration.md>), [migrations](<https://devfeed.tech/tags/migrations.md>), [rails](<https://devfeed.tech/tags/rails.md>), [relationships](<https://devfeed.tech/tags/relationships.md>), [schema](<https://devfeed.tech/tags/schema.md>), [table](<https://devfeed.tech/tags/table.md>)

### AI overview

This article explains how to identify application classes whose ActiveRecord-backed tables have a user_id attribute. It introduces a DataObject class with a .has_user_id method that can be run in the console, helping the author add belongs_to user relationships without repeatedly checking the schema.

### Source excerpt

Even though I'm a firm, firm believer in agile, I've recently been experimenting with a throwback to waterfall style development and it very quickly left me with a fully featured data structure of tables and relationships modeled as example data. What I did is very rapidly write a series of migrations and then populate them to represent a sample "installation". My goal with this approach was to play to my strengths - data modeling - and avoid getting tied down in user interface stuff (my weakness). This was a very, very interesting approach and what I found was that I got much further along the lines of the "guts" of an application simply because I never got discouraged by: Oh Shite - I know this should look good but I'm too much of a hoser to make it look good; I guess I'll put it aside and go watch TV The downside to this is that I never bothered setting up the normal associations that you do when you write a migration; I simply thought about this application in terms of the example data: the users who would be using the system the data objects that they would create how the data objects would interact with each other One of the things that I noticed when I started filling in the basics like "belongs_to :user" was that I kept constantly jumping between the class I was working on and the schema file. Finally it hit me - what I needed was a method that I could execute in the console that would tell me what classes had a user_id attribute. And so I wrote a class called DataObject (for an ActiveRecord class which stores data) and a method .has_user_id. What I was looking for was output that looked like this: > DataObject.has_user_id Initiative Yes - has a user_id field KeyResultOwner Yes - has a user_id field KeyResult Yes - has a user_id field ObjectiveOwner Yes - has a user_id field ObjectiveType Objective Yes - has a user_id field OkrTeamMember Yes - has a user_id field OkrTeam Yes - has a user_id field OrganizationGroup Organization Quarter ResponsibilityRole Stat

## Introduction to Metaprogramming in Nim

DevFeed: [Introduction to Metaprogramming in Nim](<https://devfeed.tech/articles/introduction-to-metaprogramming-in-nim-30821.md>)

Original publisher: [Read original article](<https://hookrace.net/blog/introduction-to-metaprogramming-in-nim/>)

Published: 2016-06-05T22:00:00Z

Content type: tutorial

Language: en

Sources: [Dennis Felsing](<https://devfeed.tech/sources/dennis-felsing.md>)

Topics: [Nim](<https://devfeed.tech/topics/nim.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [generics](<https://devfeed.tech/topics/generics.md>)

Tags: [article](<https://devfeed.tech/tags/article.md>), [generics](<https://devfeed.tech/tags/generics.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [nim](<https://devfeed.tech/tags/nim.md>), [programming](<https://devfeed.tech/tags/programming.md>)

### AI overview

This tutorial introduces metaprogramming in Nim and explains a progression from ordinary procedures and iterators to generic procedures, closure iterators, templates, and macros. It describes how these constructs can work with or transform program code.

### Source excerpt

Introduction to the Introduction (Meta-Introduction) Wikipedia gives us a nice description of metaprogramming: Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse and/or transform other programs, and even modify itself while running. In this article we will explore Nim's metaprogramming capabilities, which are quite powerful and yet still easy to use. After all great metaprogramming is one of Nim's main features. The general rule is to use the least powerful construct that is still powerful enough to solve a problem, in this order: Normal procs and inline iterators Generic procs and closure iterators Templates Macros So before looking at Nim's two main metaprogramming constructs, templates and macros, we'll look at what we can do with procs and iterators as well. Regular Programming Constructs Normal procs We're in normal programming land here. Regular procedures are what you know as functions elsewhere and they're pretty easy to define and use: proc sayHi(name: string) = echo "Hello ", name sayHi("World") sayHi "World" "World".sayHi Generic procs With generics we can define procs that work on multiple types. Actually a new proc will be generated based on our generic definition for each instantiation: proc min[T](x, y: T): T = if x < y: x else: y echo min(2, 3) # more explicitly: min[int](2, 3) echo min("foo", "bar") # min[string]("foo", "bar") Inline iterators Inline iterators are the default iterators in Nim. They get compiled into high performance loops: iterator reverseItems(x: string): char = for i in countdown(x.high, x.low): yield x[i] for c in "foo".reverseItems: echo c So this code gets compiled into: let x = "foo" for c in countdown(x.high, x.low): let c = x[i] echo c Of course we can make iterators generic too: iterator reverseItems[T](x: T): auto = for i in countdown(x.high, x.low): yield x[i] Closure iterators Inline iterators simultane

## What is special about Nim?

DevFeed: [What is special about Nim?](<https://devfeed.tech/articles/what-is-special-about-nim-30843.md>)

Original publisher: [Read original article](<https://hookrace.net/blog/what-is-special-about-nim/>)

Published: 2014-12-31T23:00:00Z

Content type: tutorial

Language: en

Sources: [Dennis Felsing](<https://devfeed.tech/sources/dennis-felsing.md>)

Topics: [Nim](<https://devfeed.tech/topics/nim.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Programming language](<https://devfeed.tech/topics/programming-language.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Tutorial](<https://devfeed.tech/topics/tutorial.md>)

Tags: [arrays](<https://devfeed.tech/tags/arrays.md>), [build](<https://devfeed.tech/tags/build.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [import](<https://devfeed.tech/tags/import.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [nim](<https://devfeed.tech/tags/nim.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-language](<https://devfeed.tech/tags/programming-language.md>), [run](<https://devfeed.tech/tags/run.md>), [sequences](<https://devfeed.tech/tags/sequences.md>), [syntax](<https://devfeed.tech/tags/syntax.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>), [xor](<https://devfeed.tech/tags/xor.md>)

### AI overview

An introductory article that demonstrates Nim through runnable examples, compile-time execution, CRC32 table generation, templates, macros, and language extension techniques.

### Source excerpt

Russian Translation by frol, Chinese Translation by JiyinYiyong, Japanese Translation by Mutsuha Asada The Nim programming language is exciting. While the official tutorial is great, it slowly introduces you to the language. Instead I want to quickly show what you can do with Nim that would be more difficult or impossible in other languages. I discovered Nim in my quest to find the right tools to write a game, HookRace, the successor of my current DDNet game/mod of Teeworlds. Since I'm busy with other projects for now, this blog is now officially about Nim instead, until I find time to continue developing the game. Easy to get running Ok, this part is not exciting yet, but I invite you to follow along with the post: for i in 0..10: echo "Hello World"[0..i] If you want to do so, get the Nim compiler. Save this code as hello.nim, compile it with nim c hello and finally run the binary with ./hello. To immediately compile and run, use nim -r c hello. To use an optimized release build instead of a debug build use nim -d:release c hello. With all of these settings you will see the following output: H He Hel Hell Hello Hello Hello W Hello Wo Hello Wor Hello Worl Hello World Run regular code at compile time To implement an efficient CRC32 procedure you need a lookup table. You could compute it at runtime or write it into your code as a magic array. Clearly we don't want any magic numbers in our code, so we'll do it at runtime (for now): import unsigned, strutils type CRC32* = uint32 const initCRC32* = CRC32(-1) proc createCRCTable(): array[256, CRC32] = for i in 0..255: var rem = CRC32(i) for j in 0..7: if (rem and 1) > 0: rem = (rem shr 1) xor CRC32(0xedb88320) else: rem = rem shr 1 result[i] = rem # Table created at runtime var crc32table = createCRCTable() proc crc32(s): CRC32 = result = initCRC32 for c in s: result = (result shr 8) xor crc32table[(result and 0xff) xor ord(c)] result = not result # String conversion proc $, automatically called by echo proc `$`(c: CRC32)

## Elections, in Ruby

DevFeed: [Elections, in Ruby](<https://devfeed.tech/articles/elections-in-ruby-37707.md>)

Original publisher: [Read original article](<https://carlosbecker.com/posts/elections/>)

Author: Carlos Alexandro Becker

Published: 2014-10-10T00:00:00Z

Content type: article

Language: en

Sources: [Carlos Becker](<https://devfeed.tech/sources/carlos-becker.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Script](<https://devfeed.tech/topics/script.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [script](<https://devfeed.tech/tags/script.md>), [updated](<https://devfeed.tech/tags/updated.md>)

### AI overview

A short Ruby article describing a script that parses Brazilian election results and displays the top three candidates. It also mentions metaprogramming in the String class and an update adding a second-round script.

### Source excerpt

Updated with second round script in Oct 26, 2014.

## Homoiconicity: Code as Data in Lisp Macros

DevFeed: [Homoiconicity: Code as Data in Lisp Macros](<https://devfeed.tech/articles/just-what-does-code-as-data-mean-anyway-32173.md>)

Original publisher: [Read original article](<https://adambard.com/blog/what-is-homoiconicity/>)

Published: 2014-01-19T00:00:00Z

Content type: tutorial

Language: en

Sources: [Adam Bard](<https://devfeed.tech/sources/adam-bard.md>)

Topics: [Lisp](<https://devfeed.tech/topics/lisp.md>), [syntax](<https://devfeed.tech/topics/syntax.md>), [Clojure](<https://devfeed.tech/topics/clojure.md>)

Tags: [lisp](<https://devfeed.tech/tags/lisp.md>), [macros](<https://devfeed.tech/tags/macros.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>)

### AI overview

An introductory explanation of homoiconicity in Lisp-family languages. It contrasts Lisp macros, which manipulate abstract syntax trees as values, with C-style string substitution and uses a macro example to show how code can generate code.

### Source excerpt

Recently, I've seen popular articles on macros and metaprogramming in Nimrod and Elixir. Both of these are wonderful metaprogramming systems, to be sure, but I couldn't help but imagine some in the audience smirking inwardly - and occasionally less inwardly. What odd syntax! How out-of-place they are! If only there existed a homiconic language, they think sarcastically, that could eliminate the need for awkward and forced syntax. Of course, these smug bastards are users of any of the LISP family of languages.