develooper Front page | perl.perl6.language | Postings from April 2008

Idea: infir types of constants

Thread Next
From:
John M. Dlugosz
Date:
April 12, 2008 23:03
Subject:
Idea: infir types of constants
Just surfing, I noticed something about the "D" programming language:

"
The types of constants need not be specified explicitly as the compiler 
infers their types <http://en.wikipedia.org/wiki/Type_inference> from 
the right-hand sides of assignments.

const fact_7 = Factorial!(7);
"




Now in C++, templates can be used to make function return types go along 
with their argument types, for generic arguments.

In Perl 6, I think you would have to arrange to write the return type 
later rather than sooner to do this:

   sub foo (::T $a, T $b)
   is of T

and writing it the other way around would violate the one-pass parsing.

So, how would you write the constant above in Perl 6, without knowing 
specifically what foo returned?  I'm thinking of a situation where you 
have foo as a method of a generic class, or otherwise depends on a 
generic type parameter already.  But, it could be an expression not just 
a function call, and depend on the generic types present in outer scopes 
in a less-than-simple way.

   my Type $x = $a ⊗ $b;

With C++, I would have to hope that the template writer thought to give 
the return type of that function a snappy name, rather than just 
referring to it as an inline conglomeration that involves the generic types.

The compiler knows what that type is.  How do I refer to it?  Can I say, 
"declare $x to be the type of the right-hand-side", rather than making 
me name it and checking that I got it right, or leaving it off and not 
having compile-time checking and optimization for subsequent use of $x ?

Here's an idea:  a pseudo-type of the form ::?RHSTYPE.

I could even capture it for subsequent use, like with generic type 
parameters:

   my ::?RHSTYPE ::ProdType $x = $a ⊗ $b;
   my ProdType $y = $x;

--John


Thread Next


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About