New programming paradigm
Jesse Phillips via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 7 08:36:47 PDT 2017
On Monday, 4 September 2017 at 03:26:23 UTC, EntangledQuanta
wrote:
> To get a feel for what this new way of dealing with dynamic
> types might look like:
>
> void foo(var y) { writeln(y); }
>
> var x = "3"; // or possibly var!(string, int) for the explicit
> types used
> foo(x);
> x = 3;
> foo(x);
>
> (just pseudo code, don't take the syntax literally, that is not
> what is important)
>
> While this example is trivial, the thing to note is that there
> is one foo declared, but two created at runtime. One for string
> and one for and int. It is like a variant, yet we don't have to
> do any testing. It is very similar to `dynamic` in C#, but
> better since actually can "know" the type at compile time, so
> to speak. It's not that we actually know, but that we write
> code as if we knew.. it's treated as if it's statically typed.
It is an interesting thought but I'm not sure of its utility.
First let me describe how I had to go about thinking of what this
means. Today I think it would be possible for a given function
'call()' to write this:
alias var = Algebraic!(double, string);
void foo(var y) {
mixin(call!writeln(y));
}
Again the implementation of call() is yet to exist but likely
uses many of the techniques you describe and use.
Where I'm questioning the utility, and I haven't used C#'s
dynamic much, is with the frequency I'm manipulating arbitrary
data the same, that is to say:
auto m = var(4);
mixin(call!find(m, "hello"));
This would have to throw a runtime exception, that is to say, in
order to use the type value I need to know its type.
A couple of additional thoughts:
The call() function could do something similar to pattern
matching but args could be confusing:
mixin(call!(find, round)(m, "hello"));
But I feel that would just get confusing. The call() function
could still be useful even when needing to check the type to know
what operations to do.
if(m.type == string)
mixin(call!find(m, "hello"));
instead of:
if(m.type == string)
m.get!string.find("hello");
More information about the Digitalmars-d-learn
mailing list