• entries
    359
  • comments
    237
  • views
    188610

Tangent: Pattern Matching, Part 1

Sign in to follow this  

252 views

Yet more motivation yesterday, so more Tangent advances. That, and the example code today is pretty sexy so stick around!

The first minor fix was finishing off the last little bug with generics. The basic stuff (generic fields in a type, generic parameters inferred in a method, method referring to the type's generic params) now work.


The second addition was syntax to allow for explicit declaration of generic parameters for a method. The normal (quick) way to declare a generic method in Tangent is something like:

public foo( arg) => returnVal

The explicit syntax allows for a method modifier with the syntax:

generic(generic-declaration-list)

which then looks like:

public generic(Any T) foo(T arg) => returnVal

A little less typing if you're going to refer to T a lot. It also allows for a little easier use when you need not just T, like if you want to infer the parameter from List.


The third bit of work was making sure that the type inference code I have is smart enough to deal with non-trivial inference. It turns out that it was smart enough, with no changes. Thus you can get a basic sort of pattern matching now within the language.

Which leads us to the example code of the day. Printing the default type value is currently the best way to properly detect that the types were properly inferred.


public class Printable{
public abstract ToString() => string;
}

public static default => T{
local T rtn;
return(rtn);
}

public static generic(Printable P, Printable R)
TestInfer( P -> R method ) => void{

print default

;
print default;
}

public static main()=>void{
TestInfer( (int x)=>decimal{} );
}




In the real world, this sort of thing will more often be used for method modifiers, such as this not modifier (that does not quite work yet):


public static generic(Exists P) not( P -> bool predicate ) => P -> bool{
return(
(P arg)=>bool{
return( not predicate arg );
}
);
}

public static main()=>void{
print (5 not equals 4);
}


Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now