我有这个:
data Vector3 t = Vector3 { ax, ay, az :: t }
data Point3 t = Point3 { x, y, z :: t }
data Ray3 t = Ray3 { ori :: Point3 t, dir :: Vector3 t }
data Sphere t = Sphere { center :: Point3 t, radius :: t }
Run Code Online (Sandbox Code Playgroud)
我想要一个Shape类型,所以我这样做了:
class Shape s where
distance :: s -> Ray3 t -> t
Run Code Online (Sandbox Code Playgroud)
distance获取形状和光线并计算沿给定光线到形状的距离.当我尝试创建一个实例时,它不起作用.这是我到目前为止:
instance Shape (Sphere t) where
distance (Sphere center radius) ray = …Run Code Online (Sandbox Code Playgroud) TDPL,p.167:
只要函数中的可变状态完全是暂时的(即,在堆栈上分配)和私有(即,不通过引用可能污染它的函数传递),那么该函数可以被认为是纯的.
import std.stdio : writeln;
struct M{
int[4] _data;
pure ref int opIndex(size_t i){ return _data[i]; }
}
pure M foo(ref M m){
m[0] = 1234;
return m;
}
void main(){
M m1 = M([7, 7, 7, 7]);
writeln(m1);
foo(m1);
writeln(m1);
}
// output:
// M([7, 7, 7, 7])
// M([1234, 7, 7, 7])
Run Code Online (Sandbox Code Playgroud)
可变状态是暂时的,因为它在堆栈上,对吗?但它不是私密的.那怎么foo()允许修改m1?
struct Matrix(T, size_t row, size_t col){
alias row Row;
alias col Col;
auto opBinary(string op, M)(in M m) const if(op == "*"){
static assert(Col == M.Row, "Cannot Mix Matrices Of Different Sizes.");
// whatever...
return Matrix!(T, Row, M.Col)();
}
}
void main(){
Matrix!(double, 2, 3) m1 = Matrix!(double, 2, 3)();
Matrix!(double, 3, 2) m2 = Matrix!(double, 3, 2)();
Matrix!(double, 2, 2) m3 = m1 * m2; // ERROR
// Error: cannot implicitly convert expression (m1.opBinary(m2)) of type Matrix!(double,row,col) to Matrix!(double,2,2) …Run Code Online (Sandbox Code Playgroud) 我想Maybe从D中的Haskell 实现,只是为了它的地狱.这是我到目前为止所得到的,但并不是那么好.任何想法如何改进它?
class Maybe(a = int){ } //problem 1: works only with ints
class Just(alias a) : Maybe!(typeof(a)){ }
class Nothing : Maybe!(){ }
Maybe!int doSomething(in int k){
if(k < 10)
return new Just!3; //problem 2: can't say 'Just!k'
else
return new Nothing;
}
Run Code Online (Sandbox Code Playgroud)
Haskell也许定义:
data Maybe a = Nothing | Just a
Run Code Online (Sandbox Code Playgroud) D是否具有足够表达的类型系统,以使在静态类型框架内动态工作(即,具有多个值类别)是可行的?
我问,阅读动态语言后是静态语言.示例代码(如果有)非常受欢迎.
阅读模板 - 重访:
struct S(T : T*) {
T t; // t is supposed to be of type 'int*', but it's of type 'int', why?
}
void main() {
int x = 123;
S!(int*) s;
static assert(is(typeof(s.t) == typeof(&x)));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有编译.
奇怪的是,以下编译:
struct S(T : int*) {
T t;
}
void main() {
int x = 123;
S!(int*) s;
static assert(is(typeof(s.t) == typeof(&x)));
}
Run Code Online (Sandbox Code Playgroud)
我不明白这种行为.一个解释将不胜感激.
struct S(int a, int b) { }
void fun(T)(T t) { }
Run Code Online (Sandbox Code Playgroud)
我只想和你fun一起工作S.签名约束会是什么样的?
我不能成为fun会员S,而且void fun(T)(T t) if(is(T : S)) { }我得到了Error: struct t1.S(int a,int b) is used as a type
我有以下范围:
struct Range {
uint data;
@property{
bool empty() { return false; }
uint front() { return data; }
void popFront() { data = data * 2 + 1; }
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用它,
foreach(c; Rnage()){ /*...*/ }工作,但foreach(i, c; Range()){ /*...*/ }我得到:
Error: cannot infer argument types
Run Code Online (Sandbox Code Playgroud)
我需要i像这样的东西foreach(i, v; [1,2,3,4]){ }.
D是否支持模板模板参数?如何让以下工作?
struct Type(alias a, alias b) { alias a A; alias b B; }
template MakeType(alias a, alias b)
{
alias Type!(a, b) MakeType;
}
template Foo(alias a, U) // where U is a Type
{
//...
}
template Foo(alias a, U : MakeType!(a, b), b...) // where U is a specialization
{
//...
}
Run Code Online (Sandbox Code Playgroud)
并且Foo应该被称为这样:
alias MakeType!(5, 7) MyType;
alias Foo!(5, MyType) Fooed; // error
Run Code Online (Sandbox Code Playgroud)
Error: template instance Foo!(5,Type!(5,7)) Foo!(5,Type!(5,7)) does not match template declaration Foo(alias a,U …
d ×9
haskell ×2
construct ×1
foreach ×1
range ×1
templates ×1
type-systems ×1
typeclass ×1
while-loop ×1