• entries
359
237
• views
188753

# Tangent: Pattern Matching, Part 1

221 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 theoretical not modifier:

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);}

There are no comments to display.