Jump to content
  • Advertisement
  • entries
  • comments
  • views

Tangent: Pattern Matching, Part 1

Sign in to follow this  


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:


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;

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{
(P arg)=>bool{
return( not predicate arg );

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

Sign in to follow this  


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
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!