Created
December 7, 2018 11:26
-
-
Save raph-amiard/81fefc3e3312eaef3cff31a7cee37811 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------------------------ | |
-- Formal functions version -- | |
------------------------------ | |
generic | |
type T is private; | |
-- PRO & CON: Interface is implicit & not named. Less typing to do. | |
with function Hash (Self : T) return Hash_Type is <>; | |
package Hash_Tables | |
... | |
end Hash_Tables; | |
-- PRO: Type can be anything, doesn't need to be tagged | |
type My_Type is null record; | |
function Hash (Self : My_Type) return Hash_Type; | |
-- MEH: Instantiation is simplified IFF your hash function is named exactly | |
-- the same, and you did put "is <>" in the formal subp spec in the generic. | |
-- Convenient but unsatisfactory in general, because very ad-hoc. | |
package My_Type_Hash_Tables is new Hash_Tables (My_Type); | |
-------------------------- | |
-- Tagged types version -- | |
-------------------------- | |
-- PRO & CON: Interface is explicitly specified and has a name and a set of operations | |
type Hashable is interface; | |
function Hash (Self : Hashable) return Hash_Type; | |
generic | |
-- CON: Every type instnatiating this package needs to be tagged. | |
type T is new Hashable and ...; | |
package Hash_Tables is | |
... | |
end Hash_Tables; | |
-- CON: My_Type can only have ONE hash implementation | |
type My_Type is new Hashable and Comparable and ...; | |
-- PRO: Instantiation is simplified, you don't have to pass operations | |
package My_Type_Hash_Tables is new Hash_Tables (My_Type); | |
-------------------------------- | |
-- Signature packages version -- | |
-------------------------------- | |
-- See http://www.ada-auth.org/standards/12rat/html/Rat12-4-3.html | |
-- PRO: Interface is explicit and named | |
-- CON: It's a package, not very clear that it is an interface !! | |
generic | |
type Element; | |
with function Hash (Self : Element) return Hash_Type; | |
package Hashable_Signature is end; | |
generic | |
type T is private; | |
-- CON: So verbose, so complex, don't even remember the proper syntax. | |
with package T_Hashable is new Hashable_Signature (T, <>); | |
package Hash_Tables is | |
-- PRO: Type can be anything, doesn't need to be tagged | |
-- PRO: You can have several hash implems for My_Type | |
type My_Type is null record; | |
function Hash (Self : My_Type) return Hash_Type; | |
-- CON: You have to both declare the functions and instantiate the signature | |
-- package. So verbose !! | |
package My_Type_Hashable is new Hashable_Signature (Hash => Hash); | |
-- PRO: Instantiation is simplified, you don't have to pass operations | |
package My_Type_Hash_Tables is new Hash_Tables (My_Type, My_Type_Hashable); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment