小编Arl*_*len的帖子

如何在D中实现while循环?

我知道D已经有了while循环,但由于它的高级功能,我想看看如果在代码中实现while循环会是什么样子.

动机:SO上接受的这个问题的答案.

d while-loop

5
推荐指数
2
解决办法
305
查看次数

这个类/实例有什么问题?

我有这个:

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)

haskell typeclass

5
推荐指数
1
解决办法
124
查看次数

这个纯函数如何能够修改非私有状态?

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

d pure-function

5
推荐指数
2
解决办法
269
查看次数

相同模板参数时的不同类型?

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)

d

4
推荐指数
1
解决办法
91
查看次数

如何在D中实现Haskell*Maybe*构造?

我想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)

haskell d construct

4
推荐指数
1
解决办法
239
查看次数

D是否具有足够表达的类型系统以使动态工作变得可行?

D是否具有足够表达的类型系统,以使在静态类型框架内动态工作(即,具有多个值类别)是可行的

我问,阅读动态语言后是静态语言.示例代码(如果有)非常受欢迎.

type-systems d

4
推荐指数
1
解决办法
300
查看次数

为什么这个模板参数约束不起作用?

阅读模板 - 重访:

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)

我不明白这种行为.一个解释将不胜感激.

templates d

4
推荐指数
1
解决办法
140
查看次数

泛型类型的签名约束

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

d

4
推荐指数
2
解决办法
494
查看次数

如何使用foreach语句使Range工作

我有以下范围:

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]){ }.

foreach d range

3
推荐指数
1
解决办法
226
查看次数

模板模板参数

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

3
推荐指数
1
解决办法
202
查看次数