Am 22.03.2021 um 10:10 schrieb Martin Leiser:
Am 21. März 2021 22:01:38 MEZ schrieb Ian Lance Taylor <i...@golang.org>:
On Sun, Mar 21, 2021 at 1:02 PM Martin Leiser <leiser.1...@gmail.com>
wrote:
...
import "container/list" stringlist type ElementType = string
What if I want to have a list of lists of strings?
That is a no brainer. The type "list of strings" in the example is:
"stringlist.Element". So you add the following line:
import "container/list" liststringlist type ElementType = stringlist.Element
The really tricky question is in my opinion:
What if the binding type is a parameter type itself?
type MyElement interface{ String() string }
and this is bound to
import "container/list" myelementlist type ElementType = MyElement
I.e my container type build on the list type for its implementation.
This really adds complexity to the implementation, because every import
binding "MyElement" also binds the list package parameter type.
What if I want to have a list of some unexported type that I defined
in this package?
That is the "Complicated" part of the proposal. It can be solved and it is not
that tricky:
- All the compiler really needs to know about the bound type is specified by
the bound interface type, plus the storage size.
- You have to outrule cyclic definitions, but they are impossible, because it
is the implementaion which introduces the cycle.
- You may even fall back to the normal implementation of "interface types"
behind the scenes when appropriate.
Semantically all is solved by the following three program transformations
chained together:
1. Make the private Type exported (does not change behaviour as long as you
avoid name clashes by consistent renaming)
2. Add an import to the Package defining the binding type in a renamed copy of
the generic package
3. replace the definition of the bind type by a type alias with the binding
type in the copy of step 2
So steps 2 and 3 have to take place in the proper order and recursively
across separate packages in case of parameter types used as binding types.
Every binding of "MyElement" type adds another binding to list.ElementType.
Complicated but solvable, but perhaps only if cyclic imports our
outruled for type binding imports in the first place.
Thanks again.
Ian
Thanks for your quick response.
Martin
Martin
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/26262f4f-41ad-3b4c-5682-17946174cc3a%40gmail.com.