From adb1131d08f28f0bc5f729e88b73cf22846c86c5 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 5 Feb 2021 09:01:36 -0800 Subject: Initial implementation of interface conjunctions (#1691) The basic feature here is the ability to use the `&` operator to produce the conjunction/intersection of two interfaces. That is, you can have interfaces: interface IFirst { int getFirst(); } interface ISecond { int getSecoond(); } and if you need a generic function where the type parameter `T` must conform to *both* of these interfaces, you express that by constraining the parameter to the intersection of the interfaces: void someFunction(T value) { ... } Without this feature, the main alternative an application would have is to define an intermediate interface, like: interface IBoth : IFirst, ISecond {} Forcing users to deal with an intermediate interface creates more work for type authors (they need to remember to inherit from the right combined interface(s)), or for `extension` authors (when you add `ISecond` to a type that used to just support `IFirst`, you had better also add `IBoth`). In the worst case, a family of N related "leaf" interfaces would give rise to an exponential number of intermediate interfaces to represnt the possible combinations. A conjunction like `IFirst & ISecond` is officially its own type, and can be used to declare a type alias: typealias IBoth = IFirst & ISecond; This change only includes the first pass of work on this feature, so there are several caveats to be aware of: * Using a conjunction as part of an inheritance clause is not yet supported (e.g., `struct X : IFirst & ISecond`). This is true even if the conjunction was introduced by an intermediate `typealias` * The `&` syntax introduced here is only parsed in places where only a type (not an expression) is possible. This means you cannot do things like cast to a conjunction with `(IFirst & ISecond)(someValue)`. * This work *should* apply to conjunctions of more than two interfaces (like `IA & IB & IC`) but that has not yet been tested * In the long run it may be sensible to allow conjunctions that use concrete types, but we really ought to have the semantic checking logic rule that out for now. * During testing, I encountered compiler crashes when trying to use this feature together with `property` declarations. Further investigation and debugging is called for. * The handling of conjunction types is currently incomplete, in that there are many equivalences the compiler does not yet understand. For example, it is clear that `IA & IB` is equivalent to `IB & IA`, but the compiler currently does not understand this and will treat them as different types. A deeper implementation approach is called for. * Conjunctions are currently only supported for generic type parameter constraints, when performing full specialization. Use of conjunctions for existential-type value parameters or with dynamic dispatch is not yet supported. --- source/core/slang-hash.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'source/core') diff --git a/source/core/slang-hash.h b/source/core/slang-hash.h index 5a0766c98..b2a583744 100644 --- a/source/core/slang-hash.h +++ b/source/core/slang-hash.h @@ -174,18 +174,40 @@ namespace Slang public: Hasher() {} + /// Hash the given `value` and combine it into this hash state template void hashValue(T const& value) { + // TODO: Eventually, we should replace `getHashCode` + // with a "hash into" operation that takes the value + // and a `Hasher`. + m_hashCode = combineHash(m_hashCode, getHashCode(value)); } + /// Hash the given `object` and combine it into this hash state template void hashObject(T const& object) { + // TODO: Eventually, we should replace `getHashCode` + // with a "hash into" operation that takes the value + // and a `Hasher`. + m_hashCode = combineHash(m_hashCode, object->getHashCode()); } + /// Combine the given `hash` code into the hash state. + /// + /// Note: users should prefer to use `hashValue` or `hashObject` + /// when possible, as they may be able to ensure a higher-quality + /// hash result (e.g., by using more bits to represent the state + /// during hashing than are used for the final hash code). + /// + void addHash(HashCode hash) + { + m_hashCode = combineHash(m_hashCode, hash); + } + HashCode getResult() const { return m_hashCode; -- cgit v1.2.3