Вы находитесь на странице: 1из 74

Nemerle is notable

Nemerle is notable

Denis Rystsov
twitter.com/rystsov
rystsov.denis@gmail.com

2011
Nemerle is notable

Origin
Nemerle is notable

Nemerle Experience
Nemerle is notable

Environment
Managed compiler written in Nemerle
Visual Studio 2008 plugin with debugger and autocomplete
Free IDE based on Visual Studio Isolated Shell
Sharpdevelop plugin
Mono support
NAnt plugin
Nemerle is notable

Why not try?

Nemerle is almost a superset of


C#, so if you know C# you can
use Nemerle.
Nemerle is notable

Why not try?

By the way, Nemerle's compiler


can compile C# code, so you
can try it on exising C# project
Nemerle is notable

Why not try?

Why not try?


Nemerle is notable

Part I - use your C# habits

Part I - use your C# habits


Nemerle is notable

Hello world / C#
Nemerle is notable

Hello world / Nemerle


Nemerle is notable

Extension Methods / C#
Nemerle is notable

Extension Methods / Nemerle


Nemerle is notable

LINQ / C#
Nemerle is notable

LINQ / Nemerle
Nemerle is notable

Object Initialization / C#
Nemerle is notable

Object Initialization / Nemerle


Nemerle is notable

Anonymous Types / C#
Nemerle is notable

Anonymous Types / Nemerle


Nemerle is notable

Yield return / C#
Nemerle is notable

Yield return / Nemerle


Nemerle is notable

Dynamic / C#
Nemerle is notable

Dynamic / Nemerle
Nemerle is notable

Type casting / C#
Nemerle is notable

Type casting / Nemerle


Nemerle is notable

Try..Catch / C#
Nemerle is notable

Try..Catch / Nemerle
Nemerle is notable

Partial / C#
Nemerle is notable

Partial / Nemerle
Nemerle is notable

Arrays / C#
Nemerle is notable

Arrays / Nemerle
Nemerle is notable

Attention! Wild dinosaur!


Nemerle is notable

Goto?

What is about..

GOTO?
unlike C# Nemerle doesn't support it, but..
Nemerle is notable

The proper use of goto

The only one legal usage of goto is


exit from nested loops
Nemerle provides a better
solution  named code blocks
Nemerle is notable

Named code blocks


Nemerle is notable

Beyond C#

After this short intro you can


write apps in Nemerle in C#
style.
But Nemerle goes far beyond it
Nemerle is notable

Desired C# features

It
supports
the most desired
features of the next
versions of C# right now
Nemerle is notable

Part II - desired C# features

Part II - desired C# features


Nemerle is notable

Object initializers with events


Nemerle is notable

Painless XML generation


Nemerle is notable

INotifyPropertyChanged language support


Nemerle is notable

`Safe navigation' operator


Nemerle is notable

Design by contract
Nemerle is notable

Sequence initialization (list comprehension)


Nemerle is notable

Yield from lambda


Nemerle is notable

Improved lazy support


Nemerle is notable

Part III  functional programming

Part III  functional


programming (FP)
Nemerle is notable

What is FP?

Typical signs of FP
Monads
Type inference
Pattern matching
Algebraic data types
Built-in tuple support
Local (nested) functions
Nemerle is notable

Nemerle supports FP

Nemerle supports it all


Nemerle is notable

Type inference
Nemerle is notable

Local functions
Nemerle is notable

Built-in tuple support


Nemerle is notable

Monads

Monad is creational functional programming design


pattern that deals with computation creation.

The upcoming C# 5 async/await feature is particular


case of a monad (creation of asynchronous
computation).

Computation expressions are monads + monoids.


Nemerle supports computation expressions.
Nemerle is notable

Monads

Consequently Nemerle supports


the upcoming C# 5
async/await feature right now
among the other monads.
Nemerle is notable

Example of easy composition of two async methods


Nemerle is notable

Algebraic data types

Short intro to
algebraic data types
Nemerle is notable

Algebraic data types

Let's learn ADT by example


Suppose we need to work with
expressions like

a + sin(b)
Nemerle is notable

Algebraic data type

Of couse we need trees


Nemerle is notable

OOP approach to trees (class hierarchy)


Nemerle is notable

FP approach to trees (ADT - algebraic data type)


Nemerle is notable

Algebraic data type  semantic

After we rewrite OOP code with variant (ADT) we


should consider
`Sum' as a constructor of type `Tree'
`l',`r' as its params (not a Tree.Sum's elds)

But if they are params, how get them back from a


`Tree' typed object?
Nemerle is notable

Algebraic data type  Disassembling (pattern matching)

Pattern matching solves this problem. It checks if object is


constructed with constructor specied in a pattern and if it is true
it binds actual parameters of constructor with names of variables
the pattern.

Let's see on the following example.


Nemerle is notable

Pretty Printing of `Tree' typed object


Nemerle is notable

Algebraic data type  OOP vs. FP

OOP

72 SLoC (pretty printing with


visitor pattern)
FP

25 SLoC (pretty printing with


pattern matching)
Nemerle is notable

Variant is OOP enabled algebraic data type


Nemerle is notable

Pattern matching goes beyond disassembling

Pattern matching can be nested.


Nemerle is notable

Almost all features are macros

Nemerle seems to be complex,


but. . .
Nemerle is notable

Almost all Nemerle features are macros

Don't worry
Nemerle is a simple language and almost all features are
macros  parts of standard library, not a language.

Macros is a plugin to compiler. You can write your own


macros to introduce new language constructs or eDSL.
Nemerle is notable

Almost all Nemerle features are macros

You've already meet macros in


this presentation, they are. . .
Nemerle is notable

Almost all Nemerle features are macros

LINQ Object initialization Anonymous types


Late (dynamic) XML-Literals
INotifyPropertyChanged macro Safe navigation
Design by contract Computation expressions
Lazy macro

and even

if while foreach
Nemerle is notable

Advanced macros

Beside macros copying useful features from another


languages there are macros adding unique features.

One of them is Nemerle.Peg. It is a macro-library


that allows to describe grammar in the attribute and
during the compilation necessary methods for parsing
text in this grammar will be added to the class.

Also there are some macros supporting design


patterns.
Nemerle is notable

Nemerle is cool

Nemerle is cool.
It was proved.
Nemerle is notable

Metric

Some metric of using Nemerle


in home pet long-term research
project
uniquation.com formula search
engine
Nemerle is notable

Metric

Two years ago rst prototype was rewritten from C# to


Nemerle in 3 days. Nemerle experience before 0.
Code was reduced by 46%.

Last month a new macro library (Nemerle.Peg) was applyed.


Code was reduced by 15%.
Nemerle is notable

Additional materials

Nemerle on google code

Nemerle site

Sources code of examples shown in this presentation

Вам также может понравиться