Archetype language

KennyTM~ kennytm at gmail.com
Sun Mar 20 15:46:08 PDT 2011


On Mar 21, 11 01:36, bearophile wrote:
> Through I've found a link to a language I didn't know, named Archetype. This blog post contains some pointers and references about the language:
> http://dvanderboom.wordpress.com/2010/04/26/new-language-code-named-archetype/
>
> Some comments and quotations and notes about the language. Some of the things I write are already translated to a D-like syntax.
>
> Archetype is far from being finished. It's built on top of C# and shares many things with it. C# isn't a small language.
>
> Archetype looks designed to make handy practical/industrial high level coding with GUIs, databases, etc. Archetype seems to contain many pre-built high level features, even more than C#. D seems to focus to lower level features, smaller features that you combine in many more ways.
>
> ----------------------------------
>
> The idea of optional curly braces for functions with 1 expression inside (just a return):
> int foo(int x) return x * x;
> Instead of:
> int foo(int x) { return x * x; }
>
> It is similar to a syntax like:
> foreach (x; 0 .. 10) y *= x;
> Instead of:
> foreach (x; 0 .. 10) { y *= x; }
>
> ------------------------
>
> In Archetype (and maybe C# too) delegates support the + and += operators. When you add two delegates, they get chained. When you call the result of the sum, both get called, in sequence. Probably this is handy to attach chains of callbacks to GUI elements.
>

This is a standard CLI feature. It supports -= also. No + is not supported.

> ------------------------
>
> Imports inside functions, etc.:
>
> void foo(int x) {
>      import std.stdio;
>      writeln(x * x);
> }
>

I don't think such fine-grained level of import scoping would bring many 
benefits. Module-level import is good enough.

Well maybe allow import inside unittest{} block, but we have 
version(unittest).

> ------------------------
>
> There is a very short syntax to perform the simplest loops, here translated to D-like:
>
> foreach(10) {
> }
>

Cute, but 'foreach(_; 0..10)' isn't really that long. And this change 
makes integers look like they are iterable, like

    map!"a*a"(10)

should work?!

> ------------------------
>
> In Archetype, the exclamation point can be placed before the parentheses. It is the only part of the condition that can appear outside the parentheses.
>
> if !(expression)
>      statement;
>

This makes 'if' looks like a template :p.

> ------------------------
>
> There is a form of pattern matching:
>
>
> var x = 1;
> var number = 4;
> match (number) {
>    x ->  number += 3;
>    3 | 5 ->  number++;
>    2 | 4 | 6..10 ->  {
>      number–;
>      Log("Numbers are getting too big");
>    }
> }
>

This is not pattern matching. This is only a regular 'switch' that does 
not fall-through.

     switch(number) {
         default: number += 3; break;
         case 3, 5: number ++; break;
         case 2, 4: case 6: .. case 9:
            number --;
            Log("Numbers are getting too big");
            break;
     }

Functional language's pattern matching is useful because of built-in 
algebraic types, and that they fill in the fields for you.

> ------------------------
>
> In Archetype if you have an Color enum, in some situations you don't need to write Color.Green, you are allowed to use just Green.
>
> ------------------------
>
> There are nullable types:
>
> int? x = 4;
> auto x? = 4;
>

In C# (and Archetype too, since it says 'The nullable type operator 
converts a type T to Nullable<T>, the same as in C#.'), Nullable is only 
useful for value types to provide an extra value, 'null'. Reference 
types can take 'null' no matter that '?' is present or not. This is just 
the same situation in D where structs are non-nullable.

> ------------------------
>
> There are tuples, used like this:
>
> (int, int) getMouseLocation() {
>      return (100, 50);
> }
>
>
> Or with named fields too:
>
> (x int, y int) getMouseLocation() {
>      return (100, 50);
> }
>
>
> At the call site:
>
> auto (x, y) = getMouseLocation();
>

Yeah this was tried once and becomes a mess in detail :)
http://www.digitalmars.com/d/archives/digitalmars/D/Re_Tuple_literal_syntax_Tuple_assignment_118601.html

So how Archetype deals with 0 and 1-tuple?

> ------------------------
>
> It has "Streams", that are iterators, very similar to the yield syntax sugar I have suggested:
> http://d.puremagic.com/issues/show_bug.cgi?id=5660
>
>
> yield(int) Numbers() {
>    yield 1, 2, 3;
>
> }
>
> Or even (I have kept the Archetype syntax here):
>
> yield(int) Numbers() {
>    yield 0..100 skip 5;
> }
>

'yield' is gogreatod. Most of the time an input range is sufficient, 
where a generator expresses it very nicely.

Not sure about the comprehensive version in Archetype though. This can 
be easily rewritten to

     foreach(i; 0..100 skip 5) yield i;

Often you can yield an unmodified list comprehension, you could just 
return it. There isn't much benefit in complicating the 'yield' syntax.

> ----------------------
>
> It has Pascal/Ada-style ranged numbers (I have kept the Archetype syntax here):
>
> // an int that can only have a value from 0 to 105
> type ValidAge int in [0..105];
>
>
> An anonymous subrange type, when you don't need to define the type:
> Age int in [0..105];
>

This could be a library type (as proposed before?)

     alias Bounded!(0, 105, int) ValidAge;

the problem is to let the compiler knows the value-range in the propagation.

> ----------------------
>
> It has List Comprehensions (I have kept the Archetype syntax here):
>
> var FirstHundred = [ 1..100 ];
>
> var FirstHundred = from x in [ 1..100 ] where x*x>  3 select x*2;
>
>
> In my opinion the map/filter/reduce of Phobos2 have almost failed because their syntax is too much noisy and heavy, I don't want to see them used a lot in production code. Even in Python code map/filter/reduce are uncommon. Modern Python programmers use mostly lazy/eager sequence comprehensions. But I don't like the List Comprehensions syntax of Archetype.
>

Yes. Maybe the Python/Javascript 1.7-like syntax

     a = (f(x,y,z) foreach(x,y; it) if(cond(x,y)) foreach(z; it2) ...)

we also need to be careful not to turn an invalid statement to valid.

     v = f(1,2,3)  // <--
     foreach(x, y; it);  // <--
        perform(x,y);

> ----------------------
>
> In Ruby arrays (lists) support the - infix operator too, to remove the first item equal to the one given on the right of the minus sign (similar to the list.remove() method in Python). But this operator is not present yet in Archetype design.
>

Since this is an O(N) operation I don't see any reason an operator-based 
syntax will be allowed, given 'in' is rejected for this reason.

> ----------------------
>
> A cute quote:
>
>> In my study of linguistics, I learned that legends like Noam Chomsky could learn hundreds of langauges; the previous librarian at the Vatican could read 97.<
>

Hundreds could be >> 100 :)

> ----------------------
>
> There are ways to define infix binary operators (this is dangerous/troubled stuff):
>
> // "ABC" dup 3 == "ABCABCABC"
> binary dup string (left string, right int)
> return string.Repeat(left, right)
>
>
> Even infix spaces that mean something (there was a C++0x joke about this):
>
> // "123" "45" == "12345"
> binary adjacent string (left string, right string)
> return left + right;
>
>
> And just suffixes:
>
> // 12 minutes == TimeSpan.FromMinutes(12)
> unary suffix minutes TimeSpan (short, int)
> return TimeSpan.FromMinutes((int)value);
>

"ABC".dup(3)
12.minutes    // with UFCS

(Well the did author said

     by defining these tokens as unary suffix operators, we can
     eliminate even the dot operator and make it that much more
     fluent and natural to type (without losing any syntactic
     precision).

I don't think it matters.)

> ----------------------
>
> If not specified there is a default constructor, ad in D structs:
>
> class Foo {
>    string x;
>    int y;
> }
> void main() {
>    Foo f = new Foo("hello", 10);
> }
>
> Is this going to cause problems in D?
>
> ----------------------
>
> I have omitted several other things.
>
> (Just to be sure: with this post I am not proposing to add all that stuff to D2/D3. I like only part of the things I've shown.)
>
> Bye,
> bearophile



More information about the Digitalmars-d mailing list