BOO语言和基础 5

Boo is an object-oriented, statically typed programming language for the Common

Language Infrastructure (CLI) with a Python-inspired syntax and a special focus on

language and compiler extensibility. It’s this focus on extensibility that makes it ideally

suited for building DSLs.

Boo is not just a CLR language; it’s also a JVM language–BooJay

 

http://boo.codehaus.org

特点

BooManifesto.pdf

Ø Syntactic sugar for common programming patterns

Ø Automatic variable declaration and type inference

Ø Automatic typecasting

Ø Duck typing

http://boo.codehaus.org/Language+Guide)

Boo能干哪些事的参考

http://docs.codehaus.org/display/BOO/Useful+things+about+Boo

关键特性

Boo’s language-oriented features

Ø String interpolation

name = "dear reader"

print "Hello ${name}"

Ø Is, and, not, and or

customer.CurrentPlan is null and customer.RegisteredAt>SixMonthsAgo

Ø Optional parentheses

SuggestRegisterToClub("Preferred")

SuggestRegisterToClub "Preferred"

Ø Anonymous blocks

ints = GetListOfIntegers()

ints.ForEach do(i): print i

ints.ForEach:

print "foo"

Ø Statement modifiers:turn several statements into a single sentence有:if, unless, and while.

ApplyDiscount(0.5) if customer.IsPreferred

apply_discount_of 0.5 if customer.IsPreferred

Ø Naming conventions

multiple names for a single method is to extend the Boo

compiler in such a way that it automatically translates from one naming convention to

the other

Ø Extension methods

System.String:

classStringExtensions:

[Extension]

staticdefToPascalCase(str as string):

returnchar.ToUpper(str[0]) + str.Substring(1)

使用

importStringExtensions

"some string".ToPascalCase()

Ø Extension properties

classListExtensions:

[Extension]

static Length[list as IList]:

get:

returnlist.Count

importSystem.Collections

importListExtensions

a = ArrayList()

printa.Length

Ø The IQuackFu interface

If it walks like a duck and it quacks like a duck, then it must be an IQuackFu.

Otherlanguages call it duck typing or method missing (or message not understood) and

many dynamic languages support it.

Duck typing basically means that you don’t care what the actual type of an object is.

As long as it supports the operations you need (as long as it walks like a duck), you can

treat it as a valid object (it is a duck).

Rails’ ActiveRecord:

user as User = Users.FindByNameAndPassword("foo", "bar")

That will be translated by the compiler to this:

user as User = Users.QuackInvoke("FindByNameAndPassword", "foo", "bar")

 

Boo interactive shell

Boo interactive shell by just entering booish.exe

Boo interpreter

booi test1.boo

Boo compiler

booc test1.boo

http://home.comcast.net/~brent.hughes/BooWho.htm

控制语句结构

if<BooleanExpression>:

<StatementBlock>

elif<BooleanExpression>:

<StatementBlock>

<moreelif's>

else:

<StatementBlock>

while<BooleanExpression>:

<StatementBlock>

for<Variable> in <RangeOrSequence>:

<StatementBlock>

List Array Hash

a = [4, 9.82, "Help"]

MyArray = (3, 5, 7, 9)

MyStrArray = ("one", "two", "three")

MyHash = { "Susan":3, "Melanie":9, "Cathy":27 }

 

关于DSL 可以参考前面的几个文章 

posted @ 2012-10-09 17:24  2012  阅读(2040)  评论(0编辑  收藏  举报