phelmkamp / valor Goto Github PK
View Code? Open in Web Editor NEWGo option and result types that optionally contain a value
License: MIT License
Go option and result types that optionally contain a value
License: MIT License
Check for MustOk
without IsOk
, ignoring Ok
result, etc.
See proposal: golang/go#47331
If the the above proposal is implemented, add identical methods to singleton.Set
so it implements the same interface. This will allow singleton.Set
to be used as a drop-in replacement.
Consider accepting variadic values to Errorf
like fmt.Errorf
. Find %w
in message and insert error before call to fmt.Errorf
.
Map index, channel receive, and type assertion expressions only return the extra boolean if dictated by the assignment. To use with optional.Value
you must first assign to two variables, e.g.:
var m map[string]string
v, ok := m["foo"]
val := optional.Of(v, ok)
Instead, a helper could do the assignment internally and return the populated optional.Value
, e.g.:
val := optional.OfIndex(m, "foo")
Marshaling a not-ok Value
to JSON should omit it as if it were empty paired with omitempty
. Marshaling an ok Value
should simply marshal the underlying value.
Then, during unmarshal, the Value
will be left as empty if omitted which means "not ok". Unmarshaling a present value will produce an ok Value
.
The tuple.go file is empty but has an example that shows up in the doc.
optional.Value
reads well and conveys more information.
A method to deconstruct a Value
as a separate value and bool will enable interoperability with any other "optional" types that might be out there. For example:
func (val Value[T]) Get() (T, bool) {
return val.v, val.ok
}
var other otheropt.Of(val.Get())
Alternative names:
Put T
before Tx
because Tx
can be inferred from the x
argument.
Before: optional.OfAssert[fmt.Stringer, time.Time](fmt.Stringer(time.Now()))
After: optional.OfAssert[time.Time](fmt.Stringer(time.Now()))
OfError(nil)
is currently equivalent to OfOk(<zero>)
which seems incorrect. Instead it should probably report a nil
error and a not-ok value.
This can arise when taking the result of a function that only returns error
. In that case, what should the type of T
be?
Something like:
tup := two.TupleOf(1, 2)
tup = two.TupleMap2(tup, strconv.Itoa) // returns two.Tuple[int, string]
An enum can be defined as a set of allowed values and an optional of the currently selected one.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.