go_pure

package module
v0.0.0-...-8b06204 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2020 License: MIT Imports: 7 Imported by: 0

README

go_pure

A static analysis tool for go-lang that ensures designated functions are pure.

Functions decorated by @pure is throw to purity check.

The norm of function purity is :

  • Do not refer to mutable variables that is declared outside the function scope
  • Do not call impure functions

These implies that the function always returns the same result when same arguments are passed.

Note that pure functions can have side effect, it can change the content of arguments of pointer type. It also allows print.

Example

package a

import "fmt"

// @pure
func f() {
	var gopher int32
	print(gopher)
}

var global_var int32 = 1
const global_const = 1

// @pure
func factorial(n int32) int32 {
	if n <= 0 {
		//fmt.Println("call")	// impure
		//return global_var		// impure
		return global_const		// pure
	} else {
		return n * factorial(n-1)
	}
}


// @pure
func fibonacci(n int) int {
	if n <= 0 {
		return 0;
	}
	
	previous := 0
	current  := 1
	
	for i := 1 ; i <= n ; i++ {
		next := previous + current
		previous = current
		current = next
	}
	return current
}

Install

$ go get github.com/marx-saul/go_pure/cmd/go_pure

Usage

$ go vet -vettool=`which go_pure` your-package

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "pure",
	Doc:  doc,
	Run:  run,
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer is ...

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL