Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Interoperable #74

Closed
oscbyspro opened this issue Aug 24, 2024 · 2 comments
Closed

Add: Interoperable #74

oscbyspro opened this issue Aug 24, 2024 · 2 comments
Labels
addition oh, so shiny!

Comments

@oscbyspro
Copy link
Owner

I'll add this protocol and extension for interoperability purposes (#73):

public protocol Interoperable {
    
    associatedtype Stdlib
    
    init(_ stdlib: consuming Stdlib)
        
    consuming func stdlib() -> Stdlib
    
}

extension Interoperable {

    @inlinable public var stdlib: Stdlib {
        mutating _read {
            let stdlib = self.stdlib()
            yield stdlib
            self = Self(stdlib)
        }
        
        mutating _modify {
            var stdlib = self.stdlib()
            yield &stdlib
            self = Self(stdlib)
        }
    }
}
@oscbyspro oscbyspro added the addition oh, so shiny! label Aug 24, 2024
@oscbyspro oscbyspro added this to the Ultimathnum 0.9.0 milestone Aug 24, 2024
@oscbyspro
Copy link
Owner Author

I'll conform the core integer types to it, as well as all randomness types. I'll want to conform all integer types at some point, I believe it's just a matter of getting around to implementing generic floating point conversions.

@oscbyspro
Copy link
Owner Author

oscbyspro commented Aug 24, 2024

All of the following methods generate the same machine code in -O mode:

Godbolt

public protocol Interoperable {    
    associatedtype Stdlib    
    @inlinable init(_ source: consuming Stdlib)
    @inlinable consuming func stdlib() -> Stdlib
}

extension Interoperable {
    @inlinable public var stdlib: Stdlib {
        mutating _read {
            let stdlib = self.stdlib()
            yield stdlib
            self = Self(stdlib)
        }
        
        mutating _modify {
            var stdlib = self.stdlib()
            yield &stdlib
            self = Self(stdlib)
        }
    }
}

@frozen public struct MyInt: Interoperable {
    @usableFromInline let base: Int
    @inlinable public init(_ base: Int) { self.base = base }
    @inlinable public func stdlib() -> Int { self.base }
}

func foo(a: Int, b: Int) -> (Int, Bool) {
    a.addingReportingOverflow(b)
}

func bar(a: MyInt, b: MyInt) -> (MyInt, Bool) {
    let x = a.stdlib().addingReportingOverflow(b.stdlib())
    return (MyInt(x.partialValue), x.overflow)
}

func baz(a: consuming MyInt, b: consuming MyInt) -> (MyInt, Bool) {
    let x = a.stdlib.addingReportingOverflow(b.stdlib)
    return (MyInt(x.partialValue), x.overflow)
}

oscbyspro added a commit that referenced this issue Aug 25, 2024
oscbyspro added a commit that referenced this issue Aug 25, 2024
oscbyspro added a commit that referenced this issue Aug 28, 2024
Its standard-library-compatible representation is FloatingPointSign.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition oh, so shiny!
Projects
None yet
Development

No branches or pull requests

1 participant