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 可以参考前面的几个文章 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值