GithubHelp home page GithubHelp logo

opzione's Introduction

Opzione

Opzione (Italian "option") is a Go library for optionals, with tracking of nested pointers. You can add it to your project using go get:

go get github.com/oissevalt/opzione

The package provides the optional type (container), Option. It works by dynamically checking whether the containers contain meaningful values, which in most cases means not being nil. An exception are slices, as nil slices are safe to work with. Value types will never be considered none, except when the optional is constructed by calling None, or its value is moved out with Take.

Warning

This package's content, and behaviour have not yet been stabilized. Bugs and vulnerabilities may also be present.

Option

For pointer types, Option does not only checks if the pointer it stores is nil, but also tracks nested pointers using runtime reflection. This way, it is able to report whether some nested pointers deference to nil, or a pointer is modified to be nil after Option wraps it.

package main

import (
	"fmt"

	"github.com/oissevalt/opzione"
)

func main() {
	number := 10
	numptr := &number

	option := opzione.Some(&numptr)
	fmt.Println(option.IsNone()) // false
	
	numptr = nil
	fmt.Println(option.IsNone()) // true
}

Note that the use of reflection can introduce additional time cost and memory usage, but best effort has been made to minimize such impact. According to benchmark (opzione_test.go), a sequence of operation on nested pointers took less than 200ns on average.

For value types and single pointers, Option does not enable tracking, and only checks the shallowest reference. It does not track unsafe pointers, either, because they can be arbitrarily manipulated and interpreted; there is no stable way to monitor them.

Optional

Optional is the general interface for users to define their own optional type implementation. Refer to documentation in the source code for more information.

opzione's People

Contributors

oissevalt avatar

Stargazers

 avatar Xiangze Li avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.