Boo 语言的 Duck类型简单实例

Boo语言 中文教程 全中文的 book of boo boo primiry 中文的 word文档格式 带目录 立即下载

Duck Typing ---  Duck 类型

Boo is a statically typed language, like Java or C#. This means your boo applications will run about as fast as those coded in other statically typed languages for .NET or Mono. But using a statically typed language sometimes constrains you to an inflexible and verbose coding style, with the sometimes necessary type declarations (like "x as int", but this is not often necessary due to boo's Type Inference) and sometimes necessary type casts (see Casting Types). Boo's support for Type Inference and eventually generics help here, but...

Boo是一种静态类型语言,类似Java或C#。这意味着boo程序运行速度和.net或Mono一样。但是使用静态类型语言会有一个类型不灵活,并且一种啰嗦的表达方式,必须要类似这样声明:x as int,但是在boo中没有这个必要,因为boo支持类型接口(Type Interface)。很多情况下Type Interface比泛型更加有效,但....

 

Sometimes it is appropriate to give up the safety net provided by static typing. Maybe you just want to explore an API without worrying too much about method signatures or maybe you're creating code that talks to external components such as COM objects. Either way the choice should be yours not mine.

 

 

Along with the normal types like object, int, string...boo has a special type called "duck". The term is inspired by the ruby programming language's duck typing feature ("If it walks like a duck and quacks like a duck, it must be a duck").

If you declare an object as type duck or cast to type duck (or turn on the implicit duck typing option, see below), then boo will not try to resolve any methods or operations you call on that object at compile time. Instead it will convert those operations into methods that do not resolve until runtime. So at compile time it simply trusts that you know what you are doing ("it walks like a duck so it must be a duck").

This is easier to understand by looking at some examples.

 

Illustrative Example: Static vs Duck Types  --- 静态类型和Duck类型的示例对照

This code illustrates some things you can do with duck types that you can't with static types (unless you add in casting, see Casting Types).

static1 as int  //type is fixed to be an integer
dynamic1 as duck //can be anything

static1 = 0
dynamic1 = 0
print static1+1 //-> 1
print dynamic1+1 //-> 1

#static1 = "Some string" //error, cannot cast string to int
dynamic1 = "Some string" //dynamic1 can be "any" type of thing

#print static1.ToUpper() //error, a1 is an int, not a string
print dynamic1.ToUpper() //-> SOME STRING

//You can convert a static type to a dynamic duck type:
dynamic2 as duck = static1
print dynamic2 //-> 0
dynamic2 = "Some string"
print dynamic2.ToUpper() //-> SOME STRING

//or convert a dynamic type to a static type
static2 as string = dynamic1
print static2.ToUpper()

#static3 as int = dynamic1 //error, cannot cast string to int
#print static3 + 2 

 

If it helps, whenever you see "duck", think "any" or "anything". That object can be anything, and I can do whatever to it since it is duck typed and not static typed.

A Practical Example: Automating Internet Explorer via COM Interop ---一个实际例子:通过COM接口使用Internet Explorer

import System.Threading

def CreateInstance(progid):
   type = System.Type.GetTypeFromProgID(progid)	
   return type()	

ie as duck = CreateInstance("InternetExplorer.Application")
ie.Visible = true
ie.Navigate2("http://www.go-mono.com/monologue/")

Thread.Sleep(50ms) while ie.Busy

document = ie.Document
print("${document.title} is ${document.fileSize} bytes long.")

See also how the example would look without duck typing.

Implicit Duck Typing Option (-ducky)  -----   -ducky参数 隐含duck类型

You can replace "ie as duck" in the internet explorer example with "ie" and still have it run if you turn on the implicit duck typing option. See also the examples in the tests/testcases/ducky/ folder.

You can set this option in various ways depending on which tool you are using:

  • For the booi.exe and booc.exe command line tools, add a "-ducky" option when running those tools on the command line.
  • In the Boo Interactive Interpreter, implicit duck typing is turned on by default. You can turn it off by typing "interpreter.Ducky = false".
  • In the Boo AddIn For SharpDevelop, you'll see a "duck typing by default" option by selecting from the menu Project -> Project Options, and then clicking on the configurations for your debug and release builds.

What this option does is basically make anything that is of type "object" be of type "duck" instead.

Turning on the ducky option allows you to more quickly test out code, and makes coding in boo feel much more like coding in Python. However, it slows down the speed of your application due to not using static typing. So when you are done testing, you may wish to turn the ducky option back off and add in various type declarations and casts. See Casting Types.

Regular Type Inference is still in effect even when the ducky option is turned on, so if you declare "x = 4", then x is still an int. This allows your code to still be convertible to run without the ducky option by adding in the appropriate type declarations and casts.

Advanced: intercept duck typed method calls using IQuackFu ---- 高级:使用IQuackFu接受duck类型方法

See

The -ducky option now automatically treats any class that implements the IQuackFu interface as a duck type too.

CLS Compliance  ------  CLS 编译

Your compiled assembly will still be CLS compliant (usable from other languages) if you use duck types. "Duck" is only a virtual type during the compile process. Duck types are converted into regular, CLS-compliant "object" types in the assembly. Again, see what the IE example looks like without duck typing to get an idea of how the compiler converts your code. Also, IQuackFu is a regular interface. Not special at all except to the boo compiler.

boo 语言写的拼图游戏 其实就是想挣点资源分,最近学了学boo语言,写了个拼图游戏,上传上来分享一下。程序是在SharpDevelop下开发的。 立即下载

相关推荐

RS232/RS485/CAN_BUS 通信原理总结与通信波形分析

分析一:232串口信号要点:RS232,全双工,采用三线制传输分别为TXD\RXD\GND,其中TXD为发送信号,RXD为接收信号。在RS232中任何一条信号线的电压均为负逻辑关系。即:-15v ~ -3v 代表1 +3v ~ +15v 代表0测试结果与分析:如图所示,以传输一个8位二进制数值“01101010”为例,异步传输数据的一般格式为:起始位+校验位+数据位+停止位。其中,校验位为可选项。...

乔木鱼 1万+

Boo语言

Boo 是.NET 下有和python相似语法的一种静态语言。 Boo一方面可以获取动态语言,例如类似Python这样的语言语法方面的优势,例如一些内建 数据结构;另一方面,其又是有类型的静态语言,具有速度等方面的优势。 Boo语言的介绍:http://www.infoq.com/cn/articles/boo-intro 语法手册:http://boo.codehaus.org/Boo+Prim...

wangshipeng409的博客 991

PID不够用?试试用ADRC改造你的ArduCopter:从Simulink仿真到SITL测试全记录

本文详细介绍了如何将ADRC(自抗扰控制)算法集成到ArduPilot飞控中,以替代传统PID控制器。通过Simulink建模、参数整定和SITL仿真测试,展示了ADRC在抗扰动能力、响应速度和参数调节复杂度上的优势,特别适合无人机在强风扰动下的稳定控制。

weixin_32349093的博客 282

BOO面向对象语言

BOO面向对象语言   Boo 是个面向对象语言,语法非常接近 Python,并且提供了许多可以扩充编译器的有趣特色,并且可以运行在 .NET Framework 或 Mono 上。   Boo 为开放源代码,使用MIT许可证/BSD许可证授权。 缘起   在微软推出.NET Framework之后,作者Rodrigo Barreto de Oliveira非常喜爱这个环境,却又惊讶于

痞子龙3D编程 2134

高通camx ThreadManager

m_pCore->AcceptNewJob是将一个Job或者任务插入一个Queue,有一个工作线程循环从Queue中取Job任务执行,执行任务就是回调任务中的callback函数,AcceptNewJob实现。调用AddToPriorityQueue将任务添加到queue任务队列,那么什么时候从这个queue中取任务执行呢?有个线程循环执行DoWork,最终调用checkAndDequeue取任务执行。

GetnextWindow的专栏 446

使用Dify搭建音频文件转文本工作流

使用Dify搭建音频文件转文本工作流

zyx_bx的博客 5642

Boo语言: 强大的、面向对象的编程语言

Boo是一种强大的、基于Python语法的面向对象的编程语言。它允许开发者以简洁的代码实现复杂的程序设计。Boo的设计理念是让代码易于阅读、编写和维护。 ## 功能特性 1. **强类型系统**: Boo具有静态类型检查, 可在编译时检测错误, 提高代码质量。 2. **元编程能力**: Boo支持元编程, 允许开发者自定义语言结构和扩展语言功能。 3. **表达式导向**: Boo的语法强调...

gitblog_00008的博客 748

Ubuntu20.04 安装 MATLAB R2023b 常见问题及解决方案

本文提供了Ubuntu20.04系统上安装MATLAB R2023b的完整指南,包括系统要求检查、依赖项安装、详细安装步骤及常见问题解决方案。特别针对GTK模块加载失败、OpenGL图形加速问题和中文显示乱码等常见问题提供了有效解决方法,帮助用户顺利完成安装并优化使用体验。

weixin_42619061的博客 491

Boo 语言入门教程

Boo 是一个面向对象的编程语言,设计目标是简洁、可读性强并具有高度表达性。它受到了 Python 和 C# 的启发,旨在提供一种简单但功能强大的开发体验。Boo 支持动态类型和静态类型,拥有丰富的元编程能力,使得编写编译器插件和语言扩展变得容易。该项目在 [GitHub](https://github.com/boo-lang/boo) 上开源,欢迎贡献。 ## 2. 项目快速启动 ### ...

gitblog_00832的博客 767

新手必看:瑞萨MCU选型时,QFP、QFN、BGA这些封装到底该怎么选?(附选型避坑指南)

本文详细解析了瑞萨MCU选型中QFP、QFN、BGA等封装的特点与适用场景,提供实战避坑指南。通过对比封装的空间占用、散热能力、焊接难度等核心维度,帮助工程师根据具体需求选择最合适的芯片封装,避免因选型不当导致的高成本与生产问题。

weixin_26965807的博客 426

Unreal5.6从入门到精通之 如何实现MetaHuman数字人

本文介绍了使用HYPER3D-ChatAvatar网站生成3D头部模型,并导入Unreal Engine制作数字人的完整流程。首先上传照片生成头部模型(约2分钟),设置参数后下载模型文件。然后在UE5.6中创建项目,激活MetaHuman插件,导入头部模型并调整肤色、面部特征和身体模板。通过MetaHuman Creator组装数字人蓝图,支持添加音频/视频动画功能。关键步骤包括:取消Rigging选项节省时间、切换英文模式避免材质问题、使用Live Link Face应用实现面部捕捉等。整个流程约需5-1

TxNet 1366

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Duck typing

Duck typing1. 示例2. 在静态类型语言中3. 与其他类型系统的比较3.1 结构化类型体系3.2 协议与接口3.3 模板或泛型类型4. 批评4.1 对术语本身的批评5. 其他资料6. 参考文献 本文为译文,点击此处查看原文。 在计算机程序设计中,Duck typing是Duck test的一个应用程序 —— “如果它像鸭子一样走路,像鸭子一样嘎嘎叫,那么它一定是鸭子” —— 用于确定一...

梦在哪里的博客 991

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

鸭子类型 Duck typing

今天在看Ruby的时候发现ruby有一种 duck typing 的编程风格,wiki之,贴在下面,这是属于语言思想类的东西。 在程序设计中,鸭子类型(英语:duck typing)是动态类型的一种风格。在这种风格中,一个对象有效的语义,不是由继承自特定的类

yinxusen的专栏 1673

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

C++模板:编译时模拟Duck Typing

return p?class foopublic:class barpublic:OK, nice!allclass foopublic:class barpublic:This:do noclass foopublic:class bazpublic:......public:m_o(o)private:public:

tumu_C的博客 1045

C++泛型与多态(4):Duck Typing

Duck Typing

墨1024 1176

C#黑魔法:鸭子类型(Duck Typing)

如果它走起路来像鸭子,叫起来像鸭子,那么它就是鸭子。 C#中,也有鸭子黑魔法。

bicijinlian的专栏 1890

1、何谓Duck Typing [Ruby]

问题:何谓Duck Typing? Duck Typing是否是动态语言特有的,静态类型语言中是否有类似的实现? 回答:   Duck Typing 是一种实用主义的面向对象方法,它对对象没有约定,只是在运行时进行检查,如果作为参数的对象拥有需要的方法和属性,就符合使用的要求。   “当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来像也鸭子,那个这只鸟就可以被称为鸭子。”

欢迎来到,Song的小屋 2624

Type Inference & Duck Typing

本来是要分开写Type Inference和Duck Typing的,不过后来发现Boo的官方网站已经写的比较详细了,所以我这里就把这两项特性合并起来简单介绍一下吧。 Type Inference 所谓的Type Inference(类型推断)就是根据已知条件来推断一个变量的类型,比如如果我有: str="Thisisastring" 那么变量str将被判...

100

SILENTTRINITY Boo模块开发实战:从入门到编写后渗透模块

在网络安全与渗透测试领域,命令与控制(C2)框架是实现远程访问和持久化的重要工具。其核心原理是通过客户端-服务器架构,在目标系统上执行预定义的任务模块,从而实现信息收集、横向移动等后渗透活动。从技术价值看,一个优秀的C2框架需要平衡功能、隐蔽性和可扩展性。应用场景广泛覆盖红队评估、安全研究及防御检测。其中,SILENTTRINITY框架因其独特的 **Boo语言** 模块化设计和 **无文件** 内存执行能力而备受关注。Boo作为一种语法类似Python的.NET静态类型语言,兼具开发效率与运行时性能,使得

weixin_30567225的博客 366

Go 语言编程 — 高级数据类型 — Interface、多态、Duck Typing 与泛式编程

目录 文章目录目录接口 接口 接口是 Golang 提供的一种数据类型,使用 type 和 interface 关键字来声明。接口可以把所有的具有共性的方法(Method)集合在一起,任何其他类型只要实现了这些方法就是实现了这个接口。 格式: /* 定义接口 */ type interface_name interface { method_name1 [return_type] method_name2 [return_type] method_name3 [return_type]

烟云的计算 3008

Python的类型注解

动态类型语言 = 变量没有固定类型,值才有类型;类型检查推迟到程序运行时进行。它牺牲了一部分安全性和性能,换来了极大的灵活性和开发效率。通过类型注解 + 静态检查工具(如mypy),现代开发者可以借助类型注解实现静态类型的好处,实现两全其美。理解:为什么会牺牲一部分安全性和性能1.牺牲安全性因为是动态类型,在程序运行中,可能得到非法的类型或者与开发者想要的类型不一致的类型,可能引起报错// Java(静态类型)// ✅ 正确// ❌ 编译错误!不能把 int 和 String 相加。

2403_84646142的博客 866

从软件工程的角度比较Swift、Go和Julia,我有了这些发现

作者 | Erik Engheim 译者 | 弯月 出品 | CSDN(ID:CSDNnews) 从已有代码的扩展和重用方面考虑,这几种语言的类扩展、duck type(鸭子类型)和多分发孰优孰劣? 面向对象编程(OOP)是组织大型程序的方式之一,但并不是唯一的方式。本文将从代码重用的角度比较Swift、Go和Julia。Swift采用了OOP方式,还支持接口和类扩展。Go尝试从新的角度考虑代码重用问题,在静态类型语言中引入了duck类型。而Julia拒绝使用OOP范式,而是发明了自己的范式:多分发。 .

CSDN资讯 9967

Go Interface 类型

introduction duck typing 很形象的解释了interface的本意。它是一种特别的数据类型,内部声明了一组要实现的方法集合,任何实现了这些方法的数据类型都可以认为实现了这个interface。这跟其他语言中的抽象类有异曲同工之处,但却不需要去明确声明实现这个interface。 空的interface类型没有声明任何方法,所以GO中所有数据类型都实现了interfac...

小知识折射大智慧 565

Golang中的error类型

原文:https://studygolang.com/articles/4389error类型本身就是一个预定义好的接口,里面定义了一个methodtype error interface { Error() string }生成一个新的error并返回一般有以下几种处理方式:package mainimport ( "errors" "fmt" )type Customerror stru

hyl999的专栏 3093
上一篇: Delphi 泛型集合的排序实例
下一篇: RemObjects
busintel
博客等级 码龄17年 5粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值