Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This lesson is applicable to lots of languages, not just Go. For example, looking at Swift (where the equivalent of an interface is a protocol), we find many protocols in the standard library that either have one function requirement, or add one function requirement to a parent protocol:

- Equatable requires ==

- Comparable extends Equatable, requires <

- Hashable extends Equatable, requires hash(into:)

- Identifiable requires id

- Encodable requires encode(to:)

- Decodable requires init(from:)

- CustomStringConvertible requires description

- LosslessStringConvertible extends CustomStringConvertible, requires init?(_:)

- CustomDebugStringConvertible requires debugDescription

- CaseIterable requires allCases

- Sequence requires makeIterator()

- IteratorProtocol requires next()

- TextOutputStream requires write(_:)

- TextOutputStreamable requires write(to:)

- CustomReflectable requires customMirror

- CustomPlaygroundDisplayConvertible requires playgroundDescription

- AsyncSequence requires makeAsyncIterator()

- AsyncIteratorProtocol requires next()

And Apple’s Swift-only frameworks also have major single-function protocols:

- SwiftUI.View requires body

- SwiftUI.Animatable requires animatableData

- SwiftUI.TimelineSchedule requires entries(from:mode:)

- SwiftUI.EnvironmentKey requires defaultValue

- Combine.Publisher requires receive(subscriber:)

- Combine.Cancellable requires cancel()

- Combine.Subscription requires request(_:)



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: