Lang.NET 2008/Monday
From Wiki
Contents |
[edit]
Keynote (30 minutes)
- given by Visual Studio lead, Jason Zander
- Non-NDA event
- reserved 50% of talks for non-MS talks
- goal is to share ideas, have debates
[edit]
History
- Babbage, Ada Lovelace
- Lisp, ALGOL, COBOL, FORTRAN
- BASIC, Smalltalk, PL/I
- C, Pascal
- C++, Python, Perl
- VB, PHP, Ruby, Java, Javascript
- C#, F#
[edit]
Goals for the CLR
- Modernize MS's programming interfaces
- Create consistent programming interface
- device, desktop, server, database, browser...
- Stop building tons of forms packages
- Allow developers to utilize their skills orthogonal to language choice
- provide a common platform for all languages
- CLI/CLS
- Improve productivity (RAD)
- Easy and efficient garbage collection
[edit]
Shared source
- SSCLI aka "Rotor"
- academic license
- released .NET class libraries under MS-RL
- IronPython released under "OSS" compatable license - took months
- Microsoft Permissive License (MS-PL) makes it easier to release source to tools
- ASP.NET AJAX control toolkit
- IronRuby
- Dynamic Language Runtime
- now taking contributions for IronRuby libraries (MS-PL)
[edit]
Trends
- Making web programming easier
- go for reach or rich environment?
- Silverlight is 4.5M
- integration of query logic (LINQ, P(arallel)LINQ)
- easiing integration of markup and imperaitive logic
- Parallel computing advances
[edit]
C# 3.0 (1 hour)
- Given by Anders Hejlsberg, Technical Fellow @ Microsoft
- previously at Borland
[edit]
Historical
- C# 1.0 - 1999
- Moving world to managed code
- new programming language for .NET platform
- C# 2.0
- Generics
- Catching up on things not done in 1.0
[edit]
Current
- today we're at C# 3.0
- looking at new directions in language
- not playing catch-up
- LInQ was biggest feature
- Deeper integration of data munging into language
- Increase conciceness of language
- Type inference
- Added functional programming language constructs
- Remain 100% backwards compatable with previous versions of C#
- Snuck new keywords into the language
- Isolate language from specific APIs in case one of the integrated APIs falls out of favor in the future
- Took schedule hit to ensure that this was the case
[edit]
New features
- Local variable type inference
- Lambda expressions and expression trees
- Extension methods
- Query expressions
- Automatic properties
- Anonymous types
- Object and collection initializers
- Partial methods
- 'using' now imports extension methods as well as types
- LInQ uses closures and deferred execution.
[edit]
LInQ Examples
[edit]
C# 2.0
class Program
{
static void Main(string[] args) {
Type[] types = Assembly.GetAssembly(typeof(object)).GetTypes();
List<Type> result = new List<Type>();
foreach (Type t in types)
if (t.IsInfinite) result.Add(t);
foreach (Type t in result) Console.WriteLine(t);
}
}
[edit]
C# 3.0 without 'using System.Linq;'
public static class Query
{
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) {
foreach (var item in source)
if (predicate(item)) yield return item;
}
public static IEnumerable<U> Select(<T, U>(this IEnumerable<T> source, Func<T, U> select {
foreach ( var item in source)
yield return selector(item);
}
}
public static void Print(this object obj) {
if(obj is IEnumerable && !Iobj is string)) {
foreach(var item in (IEnumerable) obj) item.Print();
}else{
Console.WriteLine(obj);
}
}
public static T[] ToArray<T>(this IEnumerable<T> source) {
return new List<T>(source);
}
}
class Program
{
static void Main(string[] args) {
var types = Assembly.GetAssembly(typeof(object)).GetTypes();
var ns = "System.Collections";
//var query = Query.Where(types, t => t.IsInterface);
//var query = Query.Select(Query.Where(types, t => t.IsInterface), t => t.Name);
/*
var query =
types
.Where(t => t.IsInterface && t.Namespace == ns)
.Select(t => t.Name);
*/
var query =
from t in types
where t.IsInterface && t.Namespace == ns
// orderby t.Name
select t.Name;
ns = "System.Reflection";
query.Print();
}
}
[edit]
C# 3.0 with 'using System.Linq;'
using System.Linq;
public static class Query
{
public static void Print(this object obj) {
if(obj is IEnumerable && !Iobj is string)) {
foreach(var item in (IEnumerable) obj) item.Print();
}else{
Console.WriteLine(obj);
}
}
}
class Program
{
static void Main(string[] args) {
var types = Assembly.GetAssembly(typeof(object)).GetTypes();
var query =
from t in types
where t.IsInterface && t.GetMembers().Count() == 1
orderby t.Name
// select new { Name = t.Name, Namespace = t.Namespace };
select new { t.Name, t.Namespace };
query.Print();
Func<double, double> f = x => (x + 1) * 2;
Console.WriteLine(f(20));
Func<double, double> f = x => (x + 1) * 2;
}
}
[edit]
Lambda Expressions, Expression trees
using System.Linq;
using System.Linq.Expression;
class Program
{
static void Main(string[] args) {
var types = Assembly.GetAssembly(typeof(object)).GetTypes();
Func<double, double> f = x => (x + 1) * 2;
Console.WriteLine(f(20));
// prints "42"
ParameterExpression x = Expression.Parameter(typeof(double), "x");
Expression body =
Expression.Call(typeof(Math).GetMethod("Sqrt"), x);
Expression<Func<double, double>> e = x => (x + 1) * 2;
Console.WriteLine(e);
// prints "x => (x + 1) * 2"
var g = e.Compile();
console.Writeline(g(20));
// prints "42"
}
}
[edit]
Future
- Declarative Programming
- Dynamic Languages
- Concurrency
The above will overlap, creating an amalgam
- More what, less how
- Thread scheduling should be done by a computer, not a developer
[edit]
DLR Vision
[edit]
Principles
- Sharing is good
- Homogeneity is bad
[edit]
IronPython examples
- He made a fancy robot thingy using XNA, the robot library and WPF
- He showed C# and Python interaction vs C and Python interaction
[edit]
New features/integration
- Visual Studio integration
- Interactive shell has completion dropdown
- Debugger plays nice with Python; shows Python stack frames as well as C# stack frames
[edit]
Dual goals
- True python implementation
- seamless integration with .net
[edit]
Things done to make dynamic language implementation easier
- Exceptional language implementation
- True to language
- True to experience
- excellent performance
- Seamless integration with .NET
- Consume .NET libs
- Interoperate with .NET languages
- Exploit .NET infrastructure
- DLR Pieces
- ExpressionTrees++
- Dynamice Sites
- Hosting API
- Utilities - Binders, Symboltables, etc
- Shared Types - BigInteger, Complex
- Expression Trees - What vs. How
def factorial(n):
if n <= 1:
return 1
else:
return n * factorial(n - 1)
- Shared Engine
- JIT Compiler
- Verifier & Security Sandbox
- GC
- DLR has done a lot of the heavy lifting so future languages do not need do it themselves
[edit]
Targeting the DLR
- IronPython on DLR
- using Ast = MSAst.Ast;
- Instead of generating IL directly, generate DLR trees
DLR trees express
- Expressions
- Statements
- Dynamic behavior support
- Factory methods
CodeBlock is a Lambda function
CodeBlock cb = Ast.CodeBlock("add2");
Variable a = cb.CreateParameter(
SymbolTable.StringToId( "a" ),
typeof(object)
);
cb.Body = Ast.Return ( ...
MSAst.Expression left = _left.Generate(tg);
MSAst.Expression right = _right.Generate(tg);
[edit]
DynamicSite
- determines types of arguments
- updates handler rule for current types
- Rules used more often move to the front of the condition checks
- Some rules may age out of delegate if they aren't used often
- re-calls handler
Create add rule
if( _op == Operators.Add ){
return Ast.Action.Operator(
Operators.Add, typeof(object),
left, right
);
}
String addition rule:
StandardRule<T> rule = new StandardRule<T>();
rule.Test = Ast.AndAlso(
Ast.TypeIs(rule.Parameters[0], typeof(string)),
Ast.TypeIs(rule.Parameters[1], typeof(string))
);
rule.MakeReturn(
this,
Ast.Call(typeof(string).GetMethod(
"Concat", new Type[]{typeof(string), typeof(string),
Ast.Convert(rule.Parameters[0], typeof(string)),
Ast.Convert(rule.Parameters[1], typeof(string))
)
);
return rule;
