swift - CS: equality of a promise in functional programming -
when 2 promises considered equal in computer science? when type same or content?
as more concrete example, have struct in swift:
struct { case value(@autoclosure () -> valuetype) case error(@autoclosure () -> errortype) case none }
and equality function:
func == <valuetype, errortype> (left: promise<valuetype, errortype>, right: promise<valuetype, errortype>) -> bool { switch (left, right) { case (.value, .value): fallthrough case (.error,.error): fallthrough case (.nil, .nil): homecoming true default: homecoming false } }
or this:
func == <valuetype, errortype> (left: promise<valuetype, errortype>, right: promise<valuetype, errortype>) -> bool { switch (left, right) { case (.value, .value): homecoming left.value() == right.value() case (.error,.error): homecoming left.error() == right.error() case (.nil, .nil): homecoming true default: homecoming false } }
which 1 right practice or depend on application?
the beautiful part equality define means yourself. if you'd simple reference equality object promises of object types, can utilize built in ===
operator. if you'd reference equality on promises themselves, same applies. or, if you're willing restructure promise , give bit more meaning, can define equality in much more interesting ways. example, i've written future<t>
bound given pthread when beingness run or has finished executing, , nil if hasn't been forced. way, equality becomes checking thread id of each executing future because there's strong chance won't same address twice pthread_create()
. bind each promise unique identifier , break relationship particular thread , define equality same uuid. or equality perversion of functional extensionality- 2 promises equal if have evaluated same form same data. sky's limit here.
swift functional-programming promise computer-science
No comments:
Post a Comment