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

Using Randomness in Stdlib methods #73

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

Using Randomness in Stdlib methods #73

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

Comments

@oscbyspro
Copy link
Owner

oscbyspro commented Aug 23, 2024

Another thing I noticed while testing (#67) is that I can't use Randomness (#45) in calls like Array/randomElement(using:). I could perhaps add some kind of adapter type to fix it:

extension Randomness {
    var stdlib: /* Adapter<Self> */ {
        get { ... }
        set { ... }
    }
}

var fuzzer = FuzzerInt(seed: 123)
var elements = [1, 2, 3]
ler random = elements.randomElement(using: &fuzzer.stdlib)!
@oscbyspro oscbyspro added the addition oh, so shiny! label Aug 23, 2024
@oscbyspro oscbyspro added this to the Ultimathnum 0.9.0 milestone Aug 23, 2024
@oscbyspro oscbyspro changed the title Randomness to Stdlib interop Randomness in Stdlib methods Aug 23, 2024
@oscbyspro
Copy link
Owner Author

oscbyspro commented Aug 23, 2024

Alternatively, perhaps I can do better with an associated type or a protocol. An associated type would be nice insofar as it lets RandomInt vend SystemRandomNumberGenerator. Fewer generic specializations, etc.

@oscbyspro
Copy link
Owner Author

Hm. I'm just thinking out loud at this point. But, maybe something like this could work:

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 added a commit that referenced this issue Aug 25, 2024
@oscbyspro oscbyspro changed the title Randomness in Stdlib methods Using Randomness in Stdlib methods Sep 6, 2024
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