Replies: 10 comments 7 replies
-
Very related: #4935 |
Beta Was this translation helpful? Give feedback.
-
True but structs and named tuples can be type compatible with many structural interfaces at once. |
Beta Was this translation helpful? Give feedback.
-
Would this be actually desugared to a behavioural interface with inlined |
Beta Was this translation helpful? Give feedback.
-
I don't think this should simply be sugar, I think it should be type information that can be checked with structinterface AStructuralInterface
a::Number
end Guarantees that
will work for any |
Beta Was this translation helpful? Give feedback.
-
Should we convert this to a "Discussion"? |
Beta Was this translation helpful? Give feedback.
-
I certainly think it's worth discussing! 😃 |
Beta Was this translation helpful? Give feedback.
-
This is interesting. It could be attractive to have generic dispatch with structural types for some uses. But there's some very thorny issues which come to mind.
More generally, what about types where the fields aren't part of the public API? I guess the advice would have to be "don't pass those kind of types to functions which use structural dispatch". But this isn't very satisfying, especially given that methods could be added at any time. |
Beta Was this translation helpful? Give feedback.
-
I think ambiguous cases ought to produce an error message like they do now
Not sure about this one
Perhaps there can be a way to assert that a type implements a structural interface when it cannot be inferred (something kind of like This could allow dispatching but leave it up to the developer to use getproperty/setproperty to implement it. |
Beta Was this translation helpful? Give feedback.
-
The given example does not necessarily indicate 'structure' to me. From a type definition perspective this looks very similar to traits to me, of which there are already numerous (competing) implementations. The main complaint for these cases, as far as I'm aware, is the amount of boilerplate they introduce. From a method definition perspective this seems related to pre- and post-condition on arguments, similar to what is implemented by something like DesignByContract.jl. Those are both, to my knowledge, aspects that are more relevant to/evaluated at run-time and I can see how it would be beneficial to pull more of this information into the type system to make it statically available and being able to expose it to tooling, as well as being able to write these things down more succinctly (both of which are things I have been giving a lot of thought over the past couple of months, but don't really have anything to share for yet). Adding structural 'inheritance' seems like a different aspect from what is being proposed here. |
Beta Was this translation helpful? Give feedback.
-
Looks like @Keno's work on interfaces will handle this! |
Beta Was this translation helpful? Give feedback.
-
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}
andWritable{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.
As @quinnj points out below, Typescript's interfaces can contain fields (part of Typescripts "structural typing" concept) as well as methods. This proposal is just for interfaces with fields, not with methods. Typescript is probably what inspired this idea in the first place.
Beta Was this translation helpful? Give feedback.
All reactions