What is the best way to program over "abstract" types in D?
Ola Fosheim Gr
ola.fosheim.grostad at gmail.com
Sat Nov 23 17:12:09 UTC 2019
On Saturday, 23 November 2019 at 13:17:49 UTC, mipri wrote:
> I read your question as "can I have typeclasses in D?"
I guess you could interpret it that way.
> I think you knowing Rust at
> all might be clouding your vision, actually.
I have hardly ever ran the Rust compiler outside of the online
one... so not very likely. Though I am keeping an eye on what
they are working on with traits and generic programming and what
is coming look promising and clean, but still incomplete in the
compiler. But yeah, Rust is getting better in the generic
department.
> These might appear in completely different modules with their
> own unrelated code, written by other people, at a later time,
> and with this instance declaration, when the whole program is
> compiled together, some integers or the 'world' abstract type
> might be handled by a lot of code that only knows that it can
> call some name() function to get a string. This code did not
> need to be edited to add a 'world' case to a switch to provide
> this functionality.
Yes, the basic idea is to be able to specify all the behaviour
for a full program without making any assumptions about binary
representation, then at a later stage choose concrete types that
can be swapped out freely without having any effect on
correctness. That would be the ideal extreme case...
So you can easily switch between many different actual concrete
implementations, debug-features, hardware types etc.
You basically write your program using concepts such as SetLike,
StringLike, ArrayLike and so on, then worry about how they should
be composed with concrete representations later.
> in a timely manner. Here's a hack, instead:
>
> Typeclass declaration? There isn't one.
That might be a bit problematic? The goal is to have strong
typing support from the compiler.
> I'm sure that someone better at D could make enormous
> improvements on this though, with non-intrusive type
> registration and typeclass declarations that you can reflect
> on, etc.
Thanks for sharing your ideas on how to do typeclasses, I think
you are onto something here.
I will probably resort to parameterising a struct as SetLike with
a ConcreteSet as the parameter and then find some way to do
matching as a start. I guess alias this can be used to "emulate"
the super type binding. But it isn't quite clear to me how to
best do it in D (or C++) yet.
More information about the Digitalmars-d-learn
mailing list