问题标签 [crystal-lang]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
crystal-lang - 设置默认属性和属性类型
我想定义一个Person
具有age
Int32 类型属性的类,并为它指定一个默认值,以防它没有提供。我知道如何做第一个:
第二个:
但不是如何做到这两点。这可能吗 ?
crystal-lang - 设置“Class”类型的属性,将其定义为 Int32、String、Float64
我有一个 class Container
,它有一个属性type
存储其中存储的元素类型:
我想像这样使用它:
但是,当运行它时,我得到:can't use Class as the type of instance variable @dtype of Crystalla::Ndarray, use a more specific type
我怎样才能做到这一点?如果我查看其他语言和库,如 numpy,它们确实在它们的 ndarray 中存储了一个类型:
我怎样才能在水晶中实现类似的东西?
编辑: dtype 本身是 python 中的一个类,但它似乎仍然像我试图实现的那样持有一个类型/类
crystal-lang - 定义类对象验证
是否有本地方法来定义 Crystal 对象的验证?让我们考虑这个类:
我怎样才能添加一个简单的验证if age < 18
?前任:
我看到一个 3rd 方库这样做,但我想避免添加依赖项。
crystal-lang - 使 Array(T) 类型的 Class 成员属性接受 T 的二维数组
我已经定义了一个Container
类。在@values 属性中,我需要存储一个数组或二维数组,这些数组中的元素可以是 Int32 或 Float64。如果我这样初始化它:
我收到一个错误:@values : Type, is inferred from assignments to it across the whole program.
如果我这样定义:
我得到:can't use Array(T) as the type of instance variable @values of Container(T), use a more specific type
我怎样才能让这个类更灵活,所以我可以做到:
crystal-lang - 对象类型的条件
我需要使用基于对象类型/类的简单 if 语句。
我有一个自定义 Array 类和一个 Matrix 类。Array 类的元素没有number_of_cols
属性
但是,运行此程序时,出现错误:
这就是为什么我有这个 if 语句的原因。我也尝试过self.responds_to?(:number_of_cols)
同样的elsif
错误。
我总是可以制作两种方法,每种类型一种,但我也想了解为什么当有专门的助手喜欢is_a?
并responds_to?
避免这些问题时这不起作用。
crystal-lang - 在 Crystal 中将十六进制字符串转换为 int
我想将变量中的十六进制字符串值转换为 int。
这适用于文字:"#{0xFF}".to_i
但是有一个 var ...?
不工作测试(插值错误):
arrays - #clone 和 #dup 有什么区别
所以我一直在学习没有红宝石背景的水晶,并注意到 api 文档有#dup
并且#clone
基本上复制了一个数组。
两者之间究竟有什么区别?api说#dup
浅拷贝数组,而#clone
深拷贝表示数组。我不确定这到底意味着什么以及我应该使用哪一个。
crystal-lang - Type checking in Crystal
I already asked a related question here and the answer did fix my issue but I have a more general misunderstanding of how crystal checks types since I keep running into similar issue so if someone could help me figure this out I'd be greatful. I've tried many things that would work great with Ruby but are absolutely not working with Crystal (I know they have many differences but I'm more familiar with Ruby).
Here is a class:
To define an array, I should do:
Which would run through set_shape
to retrieve the shape of the array (a tuple representing the number of rows and number of columns).
In this method, I check in the if statement if it's a 2D array or not.
However, when running the line above, I get this error:
Which is exactly what the first if statement should avoid. Since I'm initializing a 1D array, it shouldn't go through the .size
part.
I'm pretty sure it's a trivial explanation, and that there is something pretty basic that I didn't get but I'd like to get it instead of stumbling upon this issue all the time.
How can I check for type in Crystal since the way I'm using isn't the right way ?
I've tried using is_a?
, == Type
, using the is_matrix?
method (which works fine and does the job of determining if it's 2D or 1D but still runs through the wrong part).
EDIT: according to the first answer, I've changed the set_shape
method:
But I still have the exact same error
crystal-lang - Crystal C 绑定:返回类型 char*
我有一个带有这个签名的函数:
我的声明是:
并使用它:
如何管理返回字符串?我是否还必须导入 strlen 来测量 C 弦的长度并手动将其转换为水晶弦?
http - Crystal-lang 服务 index.html
我对这门语言有点陌生,我想开始破解一个非常简单的 HTTP 服务器。我当前的代码如下所示:
我的目标是我不想列出目录,因为这样做。index.html
如果它在 上可用,我实际上想提供服务public/
,而不必放在index.html
URL 栏中。让我们假设它index.html
确实存在于public/
。任何指向可能有用的文档的指针?