Aboriginal Generics: the future is here!

Aboriginal Generics: the future is here!

Inspired by this gem of an idea (click the image to go to the original comment):

Installation

go get github.com/vasilevp/aboriginal/cmd/aboriginal

Example Usage

command line

aboriginal -i ᐸinput_fileᐳ -o ᐸoutput_fileᐳ

You can omit both -i and -o, in which case STDIN/STDOUT will be used.

go:generate example

Create main.go with the following contents:

//go:generate aboriginal -i main.go -o main.gen.go

package main

import "fmt"

type T interface{} // template argument placeholder

type OptionalᐸTᐳ struct {
	Value T
	Valid bool
}

func main() {
	optInt := Optionalᐸintᐳ{
		Value: 42,
		Valid: true,
	}

	fmt.Printf("%T %+v\n", optInt, optInt)

	optFloat := Optionalᐸfloat64ᐳ{
		Value: 42.42,
		Valid: true,
	}

	fmt.Printf("%T %+v\n", optFloat, optFloat)
}

Then run it by calling

go generate && go run *.go

You should get the following output:

main.Optionalᐸintᐳ {Value:42 Valid:true}
main.Optionalᐸfloat64ᐳ {Value:42.42 Valid:true}

How does it work?

The algorithm is fairly simple:

  1. Parse the source file
  2. Remember all struct declarations of the form XᐸTᐳ
  3. For any reference to XᐸYᐳ, where Y is an arbitrary type, generate an implementation of XᐸTᐳ, replacing T with Y for all member types (verbatim)
  4. Additionally, if there are any known methods defined for XᐸTᐳ, generate implementations for those as well, replacing XᐸTᐳ with XᐸYᐳ in receiver type
  5. Run imports.Process() (the core of goimports) on the resulting file to fix any unused imports (necessary since all imports are copied verbatim from the original file)

TODO

  • Implement basic generic generation
  • Implement generic methods support
  • Implement proper import handling (sort of works)
  • Implement generic functions

Disclaimer

This project is a joke. Please, for the love of go, don't use in production.

Owner
Pavel Vasilev
Go and C++ developer, electronics enthusiast, hobbyist.
Pavel Vasilev
Similar Resources

Go-generics-simple-doubly-linked-list - A simple doubly linked list implemented using generics (Golang)

Welcome to Go-Generics-Simple-Doubly-Linked-List! Hi, This repository contains a

Jun 30, 2022

🌳 📂 The utility displays a tree of directories and files(symlinks in future).

dirTree The utility displays a tree of directories and files. usage: dirTree [-f] How it works with directory, where I wrote this project for example

Aug 12, 2021

Future-proof content collaboration platform

Future-proof content collaboration platform

Homepage | Dev Guide | GitHub-Repository | Issue-Tracker Pydio Cells is the nextgen file sharing platform for organizations. It is a full rewrite of t

Jan 6, 2023

Hashing algorithms simplified (supports Argon2, Bcrypt, Scrypt, PBKDF2, Chacha20poly1305 and more in the future)

PHC Crypto Inspired by Upash, also implementing PHC string format Usage Currently there are two options of using this package: Import all Import speci

Nov 27, 2022

Create production ready microservices mono repo pattern wired with Neo4j. Microservices for other languages and front end repos to be added as well in future.

Create production ready microservices mono repo pattern wired with Neo4j. Microservices for other languages and front end repos to be added as well in future.

Create Microservices MonoRepo in GO/Python Create a new production-ready project with backend (Golang), (Python) by running one CLI command. Focus on

Oct 26, 2022

Welcome to the future of programming languages: OK?

Welcome to the future of programming languages: OK?

OK? Try it out on the playground OK?'s mascot: Quentyn Questionmark. Programming Is Simple Again OK? is a modern, dynamically typed programming langua

Jan 1, 2023

Package manager for future projects

PCKGER is a package manager for my next project but when it will be able to build binaries and move libs it will be used like a normal package manager

Dec 20, 2021

GoTemplates - A Repository to store many go templates i will face in the future

Go Templates A Repository to store many go templates i will face in the future.

Jan 8, 2022

Hprose 1.0 for Golang (Deprecated). Hprose 2.0 for Golang is here:

Hprose for Golang Introduction Installation Usage Http Server Http Client Synchronous Invoking Synchronous Exception Handling Asynchronous Invoking As

Dec 15, 2022

The devs are over here at devzat, chat over SSH!

Devzat Where are the devs at? Devzat! Devzat is chat over SSH Try it out: ssh sshchat.hackclub.com Add this to ~/.ssh/config: Host chat HostName s

Jan 7, 2023

SLOs, Error windows and alerts are complicated. Here an attempt to make it easy

slo-computer SLOs, Error windows and alerts are complicated. Here's an attempt to make it easy SLO, burn_rate, error_rate, budget_spend are convoluted

Dec 30, 2022

Here is the collection of beginner friendly golang projects

Golang Mini Projects for Beginners Here is the collection of beginner friendly golang projects Mini Projects Book Store cli app to list, add, update,

Dec 29, 2022

Here is Go WebAPI

Go WebAPI About I make this Repo just for study purpose, when i was learning Go Language Go WebAPI using several third parties Web Framework, : GIN Gi

Apr 4, 2022

Golang interface for local/remote DRM CDM services (NO DRM IMPLEMENTATION HERE)

NO DRM IMPLEMENTATION HERE! ONLY ABSTRAT INTERFACE! What It's a generalized interface for different types of CDM for WEBDL use. A remote CDM JSON-RPC

Oct 24, 2022

Nuke-Net is a VERY VERY over powered and ridiculous web crawler that is well- very very noisy XD read more here

Nuke-Net is a VERY VERY over powered and ridiculous web crawler that is well- very very noisy XD read more here

Nuke-Net is a VERY VERY over powered and ridiculous web crawler that is well- very very noisy XD read more here

Dec 20, 2021

go language generics system

Gotgo This document describes the third iteration of my attempt at a reasonable implementation of generics for go based on the idea of template packag

Dec 4, 2021

Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022

Elegant generics for Go

genny - Generics for Go Install: go get github.com/cheekybits/genny ===== (pron. Jenny) by Mat Ryer (@matryer) and Tyler Bunnell (@TylerJBunnell). Un

Dec 23, 2022

go language generics system

Gotgo This document describes the third iteration of my attempt at a reasonable implementation of generics for go based on the idea of template packag

Dec 31, 2022
Comments
  • This project is not a joke

    This project is not a joke

    Just for fun, I'll leave this here:

    https://arxiv.org/pdf/2005.11710.pdf

    An actual proposal for the Generics in Go. They use monomorphisation - turning the generics into a specific type - and also the Canadian Syllabics Pa and Po.

    Of course, there's more defined semantics and so on, but what I wanted to say is: this might have inspired something! This is awesome!

    If not, they say 'great minds think the same', so there you are! :D

Welcome to the future of programming languages: OK?
Welcome to the future of programming languages: OK?

OK? Try it out on the playground OK?'s mascot: Quentyn Questionmark. Programming Is Simple Again OK? is a modern, dynamically typed programming langua

Jan 1, 2023
Here is Go WebAPI

Go WebAPI About I make this Repo just for study purpose, when i was learning Go Language Go WebAPI using several third parties Web Framework, : GIN Gi

Apr 4, 2022
Elegant generics for Go

genny - Generics for Go Install: go get github.com/cheekybits/genny ===== (pron. Jenny) by Mat Ryer (@matryer) and Tyler Bunnell (@TylerJBunnell). Un

Dec 23, 2022
go language generics system

Gotgo This document describes the third iteration of my attempt at a reasonable implementation of generics for go based on the idea of template packag

Dec 31, 2022
An implementation of standard generics APIs in Go.

generics This package shows an implementation outlook of proposed generics APIs import "changkun.de/x/generics" Related issues: golang/go#45458 golang

Dec 5, 2022
Go 1.18 Generics based slice package

The missing slice package A Go-generics (Go 1.18) based functional library with no side-effects that adds the following functions to a slice package:

Jan 8, 2023
Advent of Code 2021 solutions using Go 1.18 Generics

advent-of-code-2021 Here are my solutions for Advent of Code 2021. This year, I chose to write my solutions using Go 1.18 with generics (by building t

Dec 18, 2022
Go-generic - A collection of experiments using Go Generics coming out in Go 1.18

Go Generic - experiments with Go 1.18 beta Data structures: iter.Iter[T any] - l

Aug 15, 2022
Generic-list-go - Go container/list but with generics

generic-list-go Go container/list but with generics. The code is based on contai

Dec 7, 2022
Placeholder for the future project (lets-go-chat)Placeholder for the future project (lets-go-chat)

Placeholder for the future project (lets-go-chat)Placeholder for the future project (lets-go-chat)

Jan 10, 2022