Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Structural Interfaces (not behavioral) #43083

Closed
zot opened this issue Nov 15, 2021 · 6 comments
Closed

Structural Interfaces (not behavioral) #43083

zot opened this issue Nov 15, 2021 · 6 comments

Comments

@zot
Copy link

zot commented Nov 15, 2021

I think it would be helpful if there were "structural interfaces" to express that a value is expected to have readable or writable fields of certain types when the value could also contain other fields (so the structural interface could be a subset of the value's type). Maybe something like Readable{x::Int, y::String} and Writable{z::Float64} (those names would probably clash with a lot of code that's already out there). This is a way to sort of embrace Julia's duck typing within the type system (it's no coincidence that TypeScript's interfaces can contain fields). This isn't like Java's interfaces because it doesn't address methods but I think it would provide some important utility.

This would let a function author indicate, "this function will fail if the input does not have an x::Int field," and IDEs could warn developers when they try to pass incompatible data to the function.

This could help document data used in protocols, file formats, etc. but still allow developers the freedom to use structures or named tuples which contain more information than the function uses.

@JeffBezanson
Copy link
Sponsor Member

Very related: #4935

@zot
Copy link
Author

zot commented Nov 16, 2021

True but structs and named tuples can be type compatible with many structural interfaces at once.

@iago-lito
Copy link

Would this be actually desugared to a behavioural interface with inlined get_field_x() -> Int and set_field_x(Int) methods?

@zot
Copy link
Author

zot commented Jan 10, 2022

I don't think this should simply be sugar, I think it should be type information that can be checked with isa, etc. so that this:

structinterface AStructuralInterface
  a::Number
end

Guarantees that a exists in anything implementing the interface and

function foo(x::AStructuralInterface)
	println(x isa AStructuralInterface)
	println(x.a)
end

will work for any x that implements AStructuralInterface, printing true and the value of a.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Jan 12, 2022

Should we convert this to a "Discussion"?

@zot
Copy link
Author

zot commented Jan 24, 2022

I certainly think it's worth discussing! 😃

@JuliaLang JuliaLang locked and limited conversation to collaborators Jan 24, 2022
@vchuravy vchuravy converted this issue into discussion #43912 Jan 24, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants