当前位置:网站首页>Pony语言学习(六):Struct, Type Alias, Type Expressions
Pony语言学习(六):Struct, Type Alias, Type Expressions
2022-08-10 05:29:00 【溴锑锑跃迁】
写在前面的:
本节我们就要结束类型的学习了。就剩下三个方式了:Struct, Type Alias, Type Expressions。其中我们会涉及一些比较新的概念:C FFI(Foreign Function Interface):https://tutorial.ponylang.io/c-ffi.html、Union/Intersection Type。
1.Structs:
你不会经常用到Struct的,why?因为它是用来沟通Pony语言和C语言的。除此之外,它和类别无二样:
A
struct
is a class like mechanism used to pass data back and forth with C code via Pony’s Foreign Function Interface.Like classes, Pony structs can contain both fields and methods. Unlike classes, Pony structs have the same binary layout as C structs and can be transparently used in C functions. Structs do not have a type descriptor, which means they cannot be used in algebraic types or implement traits/interfaces.
把关键字class换成关键字struct后,一切class的规则都可以套用,此处不再赘言。我们以后在讲C FFI时,会来一个overview和further study。
2.Type Alias:
先来翻译一下:alias n.别名,假名,又名。
我们前面说过,原类有一个用途是记号。请看下面这段代码:
primitive Red
primitive Blue
primitive Green
type Color is (Red | Blue | Green)
‘|’这个运算符的含义大家都明白,||代表或运算。现在Color这个类型就可以称作是Union Type,but what's it?
A union type is a form of closed world type. That is, it says every type that can possibly be a member of it.
话回正题,type alias的标志就是关键字type,其概念的核心就是“等价”,意思是type后的新类型是(is)后面的类型。
--复合类型(Complex types):
上例子:
interface HasName
fun name(): String
interface HasAge
fun age(): U32
interface HasAddress
fun address(): String
type Person is (HasName & HasAge & HasAddress)
用&来表示的类型,我们叫intersection type。
我们再来深入理解一下:
Union type(connected by "|"):
这是一种类型集合,类型产生的实例其类型为集合元素中的一种
Intersection type(connected by "&"):
这是一种类型集合,类型产生的实例其类型为集合元素中的全部
3.Type Expressions
Tuple(元组):
A tuple type is a sequence of types
我们这样定义一个元组:
var x : (String, U64)
x = ("hi", 3)
x = ("bye", 7)
可以这样赋值:
(var a, var b) = x
也可以直接访问该元组的元素:
var a = x._1
var b = x._2
Why use a tuple instead of a class? Tuples are a way to express a collection of values that doesn’t have any associated code or expected behaviour. Basically, if you just need a quick collection of things, maybe to return more than one value from a function, for example, you can use a tuple.
Combining type expressions :
type Number is (Signed | Unsigned | Float)
type Signed is (I8 | I16 | I32 | I64 | I128)
type Unsigned is (U8 | U16 | U32 | U64 | U128)
type Float is (F32 | F64)
嗯哼?!
Is
Number
a type alias for a type expression that contains other type aliases? Yes! Fun, and convenient.
实际上,上述概念属于集合类型。这方面我认为Ceylon是主打,就像Pony主打并发及其数据安全。有兴趣的同学可以看看Ceylon的集合类型处理,和Pony的集合类型进行对照学习,当然Pony这部分只是实现了“现代编程语言”的一个接口,不能奢望太多。附上Ceylon网站:https://ceylon-lang.org/.对此,王垠也给出了他的意见:
写在后面的:
接下来我们会继续唠唠语法,if语句啊,for语句之类的。我打算在一篇文章里结束对Expression的介绍。总之,感谢一直以来对Pony的支持!
欢迎加入Pony语言QQ群:261824044!
边栏推荐
- Abstract problem methodology
- OneFlow源码解析:算子指令在虚拟机中的执行
- 一篇文章带你搞懂什么是幂等性问题?如何解决幂等性问题?
- OAuth2的使用场景、常见误区、使用案例
- CORS跨域资源共享漏洞的原理与挖掘方法
- 如何取得某月的最后一天
- Thread.sleep, Thread.yield role explanation
- Ask you guys.The FlinkCDC2.2.0 version in the CDC community has a description of the supported sqlserver version, please
- 【静态代理】
- 线性代数(四)
猜你喜欢
【裴蜀定理】CF1055C Lucky Days
How to simulate the background API call scene, very detailed!
8.STM32F407之HAL库——PWM笔记
论文精度 —— 2016 CVPR 《Context Encoders: Feature Learning by Inpainting》
Become a language that hackers have to learn. Do you think it's okay after reading it?
基于Qiskit——《量子计算编程实战》读书笔记(四)
Interface documentation evolution illustration, some ancient interface documentation tools, you may not have used it
手把手带你写嵌入式物联网的第一个项目
Buu Web
一文带你搞懂OAuth2.0
随机推荐
awk of the Three Musketeers of Shell Programming
Mysql CDC (2.1.1) inital snapshot database set up five concurrent degree, se
Jenkins 如何玩转接口自动化测试?
When oracle cdc, set the parallelism to 2 and the number of slots to 1, and the final task has only one tm. Is it because oracle does not support concurrency
SQL Server查询优化
You can‘t specify target table ‘kms_report_reportinfo‘ for update in FROM clause
CORS跨域资源共享漏洞的原理与挖掘方法
Arduino框架下合宙ESP32C3 +1.8“tft 网络时钟
FPGA工程师面试试题集锦1~10
抽象问题方法论
MySQL simple tutorial
Order table delete, insert and search operations
Flutter development: error The following assertion was thrown resolving an image codec: Solution for Unable to...
FPGA engineer interview questions collection 21~30
Linear Algebra (4)
flinksql怎么写redis的value只有最后一个字段?
aliases节点分析
MySql's json_extract function processes json fields
SQL database field to append to main table
OneFlow源码解析:算子指令在虚拟机中的执行