CSharp Similarities and Differences
This document lists some basic differences between Nemerle and C# in a terse form. If you know Java or C++ it should still be fairly helpful.
Changes In Expressions
C# | Nemerle | Remarks |
---|---|---|
const int x = 3; |
def x : int = 3; |
Variables defined with def cannot be changed once defined. This is similar to readonly or const in C# orfinal in Java. Most variables in Nemerle aren't explicitly typed like this. |
int x = 3; |
mutable x : int = 3; |
Variables defined with mutable can be changed once defined. Most variables in Nemerle aren't explicitly typed like this. |
var = 3; //Will compile. |
def = 3;//Will compile! |
Nemerle's type inference is lightyears ahead of C#'s. If there is clear evidence of a variable's type, there's a 99% chance Nemerle will infer it. |
int a = b = c; |
def a = c; |
The type of the assignment operator is void. |
value = cond ? var1 : var2; |
value = if(cond) var1 else var2 |
No ternary operator is needed as everything is an expression in Nemerle. The 'else' branch is mandatory here! (Don't panic! if-without-else has its own keyword.) |
Class myClass = new Class(parms); |
def myClass = Class(parms); |
Nemerle doesn't require new when calling a constructor. |
Book[] books = new Book[size]; |
def books = array(size) : array[Book]; |
Often the array type can be inferred and this is simplified; as in the next example. |
Book[] books = new Book[size]; |
def books = array(size); |
When the type can be inferred from context or later use (which is most of the time), you can drop the type declaration |
int[] numbers = {1, 2, 3}; |
def numbers = array[1, 2, 3]; |
Initializing an array. Without the array keyword this would create a list. |
int[,] numbers = new int[2,3]; |
def numbers = array(2,3) : array.[2][int]; |
Multidimensional array constructor. The type can usually be inferred from use and not declared. |
int[,] numbers = { {1,2,3}, {1,4,9} }; |
def numbers = [ [1,2,3], [1,4,9] ]; |
Multidimensional array initialization. |
new {Prop1 = 1; Prop2 = "string"} |
using Nemerle.Extensions; |
Nemerle anonymous typesare a bit more flexible (e. g. can be generic or returned from a method). They must be imported from Nemerle.Extensions however. |
new Class { |
Class() |
The Nemerle Object Modifier macro is more powerful. |
if(cond) |
when(cond) |
if without else is called when . Nemerle requiresif statements to be paired with else for clarity. |
if(!cond) |
unless(cond) |
In Nemerle,if(!cond) can use the clearerunless(cond) syntax. Of course,when(!cond) can also always be used. |
if (cond) |
match(cond){ |
Pattern Matchingprovides a clearer way of delegating control flow. |
if (cond) |
using Nemerle.Imperative; |
Alternately the Imperative namespace may be imported. This isdiscouragedhowever. |
try {...} |
try {...} |
Nemerle's somewhat differenttry ... catch syntax is consistent with its pattern matching syntax. |
(type) expr |
expr :> type |
Runtime type cast, allows for downcasts and upcasts. |
(type) expr |
expr : type |
Static cast, only upcasts are allowed. |
using System; |
using System; |
In Nemerle, you can apply the using directive to classes as well as namespaces. Opened namespaces allow you to drop the prefix of other namespaces, likeSystem inSystem.Xml . More info. |
using System.Windows.Forms; Button button = control as Button; if (button != null) |
match (control) { |
as can be simulated withmatch . It is a bit more to type up in simple cases, but in general Nemerle's construct is more powerful. |
int y = x++; |
def y = x; |
The ++ and -- operators return void, just like assignment. So, both prefix and postfix versions are equivalent. |
Changes In Type Definitions
C# | Nemerle | Remarks |
---|---|---|
static int foo (int x, string y) |
static foo (x : int, y : string) : int |
Types are written after variable names. |
class Foo { |
class Foo { |
The constructor's name is alwaysthis . |
class Foo { |
class Foo { |
There is no special syntax for the destructor, you just override theFinalize method. |
class Foo : Bar { |
class Foo : Bar { |
The base constructor is called in the constructor's function body. |
class Foo { |
class Foo { |
Fields which will be changed outside of the constructor need to be marked asmutable . |
class Foo { |
class Foo { |
Read-only/const are used by default. |
class Foo { |
class Foo { |
Static variable. |
class Foo { |
module Foo { |
A module is a class in which all members are static. |
using System.Runtime.CompilerServices.CSharp; class C { |
class C { |
Indexers. |
C# | Nemerle |
---|---|
When two interfaces use the same method to perform different functions, different names can be given to each method. | |
interface SpeaksEnglish{ |
interface SpeaksEnglish{ |
Generics
C# | Nemerle | Remarks |
---|---|---|
class A { T x; } |
class A [T] { x : T; } |
Type parameters are written in square brackets [...]. |
typeof(A); |
typeof(A[_,_]); |
typeof expression |
New Stuff
Nemerle contains many constructs which are not present in C#. Unfortunately, most of them don't really fit into a side-by-side comparison format:
- Tuples -- a nameless, heterogeneous data structure.
- Lists -- a special syntax for lists and list processing.
- The Void Literal -- a useful construct for recursive functions.
- Local functions -- defining functions within other functions.
- Functional Values -- passing functions as arguments and returning them from other functions.
- Anonymous Functions -- defining functions which don't need names.
- Variants and Pattern Matching -- an alternative, and very useful, control flow construct.
- Macros -- writing code that writes code.
Other Minor Differences
Ambiguity Isn't Tolerated
namespace YourAttributes{
class Serializable : System.Attribute { }
}
namespace MyAttributes{
using YourAttributes;
class Serializable : System.Attribute { } [Serializable] class SomeClass { }
}
C# compilers will choose MyAttributes.Serializable or, if its definition is commented out, YourAttributes.Serializable. Nemerle will raise an error telling you to be more specific about which attribute you want to use.
Exclusion of Overridden Methods
class BaseClass
{
public virtual AddItem (val : string) : void { }
} class TestClass : BaseClass
{
public AddItem (val : object) : void { }
public override AddItem (val : string) : void { }
}
...
TestClass().AddItem ("a"); // C# will choose TestClass.AddItem (object)
// Nemerle will choose TestClass.AddItem (string)
This behaviour comes from section 7.6.5.1 of the C# specification, which states "...methods in a base class are not candidates [for overload resolution] if any method in a derived class is applicable (§7.6.5.1)." Unfortunately, this rule is patently absurd in situations like the above. The Nemerle compiler always chooses the method whose signature best matches the given arguments.
CSharp Similarities and Differences的更多相关文章
- The Similarities and Differences Between C# and Java -- Part 1(译)
原文地址 目录 介绍(Introduction) 相似点(Similarities) 编译单位(Compiled Units) 命名空间(Namespaces) 顶层成员(类型)(Top Level ...
- Comparing the MSTest and Nunit Frameworks
I haven't seen much information online comparing the similarities and differences between the Nunit ...
- A Brief Review of Supervised Learning
There are a number of algorithms that are typically used for system identification, adaptive control ...
- TIJ——Chapter One:Introduction to Objects
///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...
- scala vs java 相同点和差异
本贴是我摘抄自国外网站,用作备忘,也作为分享! Similarities between Scala and Java Following are some of the major similari ...
- Gestures_Article_4_0
Technical Article Windows Phone 7™ Gestures Compared Lab version: 1.0.0 Last updated: August 25, 201 ...
- 15 things to talk about in a healthy relationship
15 things to talk about in a healthy relationship男女交往中可以谈论的15个话题 1. Your Daily Activities 1. 你的日常活动 ...
- .net程序员必须知道的知识
A while back, I posted a list of ASP.NET Interview Questions. Conventional wisdom was split, with ab ...
- SICP阅读笔记(一)
2015-08-25 008 Foreword QUOTE: I think that it's extraordinarily important that we in compute ...
随机推荐
- 2015.01.15(android AsyncTask)
参考网址:http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html /* * Params 启动任务执行的输入参数,比如HTT ...
- E2PROM与Flash的引脚图
E2PROM(24C02):
- 介绍“Razor”— ASP.NET的一个新视图引擎
我的团队当前正在从事的工作之一就是为ASP.NET添加一个新的视图引擎. 一直以来,ASP.NET MVC都支持 “视图引擎”的概念—采用不同语法的模板的可插拔模块.当前ASP.NET MVC “默认 ...
- [转]JEXUS的高级配置
转自:http://www.cnblogs.com/xiaodiejinghong/archive/2013/04/14/3019660.html 前一回合,我们对服务器软件Jexus作了简单的介绍, ...
- Hibernate,JPA注解@EmbeddedId
定义组合主键的几种语法: 将组件类注解为@Embeddable,并将组件的属性注解为@Id 将组件的属性注解为@EmbeddedId 将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为 ...
- oracle 执行执行动态存储过程名---其实就是存储过程名是个字符串参数
假设我有一个过程P1(V1 IN VARCHAR2),另一有一个过程EX(P IN VARCHAR2,P IN VARCHAR2),第一个参数是过程名,第二个参数是指定过程的参数,我执行EX('P1' ...
- clip属性
clip:rect矩形剪裁功能及一些应用介绍. 其实是这样的,top right bottom left分别指最终剪裁可见区域的上边,右边,下边与左边.而所有的数值都表示位置,且是相对于原始元素的左上 ...
- Unity脚本在层级面板中的执行顺序测试2
上一篇测试了生成顺序对执行顺序的影响,链接:LINK 执行顺序测试3: LINK 这篇主要测试一下Awake,OnEnable,Start三个常用消息的循环顺序 1.测试消息循环顺序 先上一个最简单的 ...
- Behavior Designer中Wait节点的坑
某一组行为放在并行节点下,并且包含Wait节点动作.当等待时间不达到时它会返回Runing 造成整个行为树阻塞 应该考虑写一个CD时间装饰器来解决此类问题,当CD时间未到返回Failure
- JS中的call()和apply()方法和bind()
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...