Jump to page: 1 26  
Page
Thread overview
static arrays becoming value types
Oct 20, 2009
Walter Bright
Oct 20, 2009
Jason House
Oct 20, 2009
grauzone
Oct 20, 2009
Robert Jacques
Oct 20, 2009
Walter Bright
Oct 20, 2009
dsimcha
Oct 20, 2009
Leandro Lucarella
Oct 20, 2009
language_fan
Oct 20, 2009
Leandro Lucarella
Oct 20, 2009
language_fan
Oct 20, 2009
Robert Jacques
Oct 21, 2009
language_fan
Oct 21, 2009
Robert Jacques
Oct 21, 2009
language_fan
Oct 21, 2009
Bill Baxter
Oct 21, 2009
language_fan
Oct 21, 2009
language_fan
Oct 21, 2009
language_fan
Oct 21, 2009
language_fan
Oct 21, 2009
Denis Koroskin
Oct 21, 2009
grauzone
Oct 21, 2009
Leandro Lucarella
Oct 21, 2009
grauzone
Oct 21, 2009
Leandro Lucarella
Oct 21, 2009
language_fan
Oct 21, 2009
Leandro Lucarella
Oct 21, 2009
bearophile
Oct 21, 2009
Leandro Lucarella
Oct 21, 2009
Robert Jacques
Oct 21, 2009
Robert Jacques
Oct 21, 2009
Leandro Lucarella
Oct 21, 2009
Robert Jacques
Oct 21, 2009
Yigal Chripun
Oct 21, 2009
Leandro Lucarella
Oct 20, 2009
downs
Oct 20, 2009
Saaa
Oct 20, 2009
Walter Bright
Oct 20, 2009
Chad J
Oct 20, 2009
Yigal Chripun
Oct 20, 2009
Don
Oct 20, 2009
bearophile
Oct 20, 2009
Don
Oct 20, 2009
Ary Borenszweig
Oct 20, 2009
Max Samukha
Oct 20, 2009
dsimcha
October 20, 2009
Currently, static arrays are (as in C) half-value types and half-reference types. This tends to cause a series of weird problems and special cases in the language semantics, such as functions not being able to return static arrays, and out parameters not being possible to be static arrays.

Andrei and I agonized over this for some time, and eventually came to the conclusion that static arrays should become value types. I.e.,

  T[3]

should behave much as if it were:

  struct ??
  {
     T[3];
  }

Then it can be returned from a function. In particular,

  void foo(T[3] a)

is currently done (as in C) by passing a pointer to the array, and then with a bit of compiler magic 'a' is rewritten as (*a)[3]. Making this change would mean that the entire array would be pushed onto the parameter stack, i.e. a copy of the array, rather than a reference to it.

Making this change would clean up the internal behavior of types. They'll be more orthogonal and consistent, and templates will work better.

The previous behavior for function parameters can be retained by making it a ref parameter:

   void foo(ref T[3] a)
October 20, 2009
Walter Bright Wrote:

> Currently, static arrays are (as in C) half-value types and half-reference types. This tends to cause a series of weird problems and special cases in the language semantics, such as functions not being able to return static arrays, and out parameters not being possible to be static arrays.
> 
> Andrei and I agonized over this for some time, and eventually came to the conclusion that static arrays should become value types. I.e.,
> 
>    T[3]
> 
> should behave much as if it were:
> 
>    struct ??
>    {
>       T[3];
>    }
> 
> Then it can be returned from a function. In particular,
> 
>    void foo(T[3] a)
> 
> is currently done (as in C) by passing a pointer to the array, and then with a bit of compiler magic 'a' is rewritten as (*a)[3]. Making this change would mean that the entire array would be pushed onto the parameter stack, i.e. a copy of the array, rather than a reference to it.
> 
> Making this change would clean up the internal behavior of types. They'll be more orthogonal and consistent, and templates will work better.
> 
> The previous behavior for function parameters can be retained by making it a ref parameter:
> 
>     void foo(ref T[3] a)

I've never heard the argument why they should be value types. Can you or Andrei explain why it makes more sense as value types?
October 20, 2009
On Mon, 19 Oct 2009 21:50:46 -0400, Walter Bright <newshound1@digitalmars.com> wrote:
> Currently, static arrays are (as in C) half-value types and half-reference types. This tends to cause a series of weird problems and special cases in the language semantics, such as functions not being able to return static arrays, and out parameters not being possible to be static arrays.
>
> Andrei and I agonized over this for some time, and eventually came to the conclusion that static arrays should become value types. I.e.,
>
>    T[3]
>
> should behave much as if it were:
>
>    struct ??
>    {
>       T[3];
>    }
>
> Then it can be returned from a function. In particular,
>
>    void foo(T[3] a)
>
> is currently done (as in C) by passing a pointer to the array, and then with a bit of compiler magic 'a' is rewritten as (*a)[3]. Making this change would mean that the entire array would be pushed onto the parameter stack, i.e. a copy of the array, rather than a reference to it.
>
> Making this change would clean up the internal behavior of types. They'll be more orthogonal and consistent, and templates will work better.
>
> The previous behavior for function parameters can be retained by making it a ref parameter:
>
>     void foo(ref T[3] a)

Thank you. Will the various operators be overloaded? i.e. a = b + c; instead of a[] = b[] + c[]; ?
October 20, 2009
== Quote from Walter Bright (newshound1@digitalmars.com)'s article
> Currently, static arrays are (as in C) half-value types and
> half-reference types. This tends to cause a series of weird problems and
> special cases in the language semantics, such as functions not being
> able to return static arrays, and out parameters not being possible to
> be static arrays.
> Andrei and I agonized over this for some time, and eventually came to
> the conclusion that static arrays should become value types. I.e.,
>    T[3]
> should behave much as if it were:
>    struct ??
>    {
>       T[3];
>    }
> Then it can be returned from a function. In particular,
>    void foo(T[3] a)
> is currently done (as in C) by passing a pointer to the array, and then
> with a bit of compiler magic 'a' is rewritten as (*a)[3]. Making this
> change would mean that the entire array would be pushed onto the
> parameter stack, i.e. a copy of the array, rather than a reference to it.
> Making this change would clean up the internal behavior of types.
> They'll be more orthogonal and consistent, and templates will work better.
> The previous behavior for function parameters can be retained by making
> it a ref parameter:
>     void foo(ref T[3] a)

Vote++.  It's funny, I use static arrays so little that I never realized that they weren't passed by value to functions.  I'd absolutely love to be able to just return static arrays from functions, and often use structs to do that now, but using structs feels like a really ugly hack.
October 20, 2009
Jason House wrote:
> Walter Bright Wrote:
> 
>> Currently, static arrays are (as in C) half-value types and half-reference types. This tends to cause a series of weird problems and special cases in the language semantics, such as functions not being able to return static arrays, and out parameters not being possible to be static arrays.
>>
>> Andrei and I agonized over this for some time, and eventually came to the conclusion that static arrays should become value types. I.e.,
>>
>>    T[3]
>>
>> should behave much as if it were:
>>
>>    struct ??
>>    {
>>       T[3];
>>    }
>>
>> Then it can be returned from a function. In particular,
>>
>>    void foo(T[3] a)
>>
>> is currently done (as in C) by passing a pointer to the array, and then with a bit of compiler magic 'a' is rewritten as (*a)[3]. Making this change would mean that the entire array would be pushed onto the parameter stack, i.e. a copy of the array, rather than a reference to it.
>>
>> Making this change would clean up the internal behavior of types. They'll be more orthogonal and consistent, and templates will work better.
>>
>> The previous behavior for function parameters can be retained by making it a ref parameter:
>>
>>     void foo(ref T[3] a)
> 
> I've never heard the argument why they should be value types. Can you or Andrei explain why it makes more sense as value types?

Because they are some bastardization now. Neither slice, nor reference type, nor value type. Somehow, they behave like value type when declared in a struct or a variable. (Because they allocate memory for the contents they point to.) But if you pass them as argument, they behave like reference type (they're just pointers to the actual data). And you can't return them at all from functions for unknown reasons. It's a real WTF.

The only clean way to fix them is the as described by Walter.
October 20, 2009
Walter Bright wrote:
> Currently, static arrays are (as in C) half-value types and half-reference types. This tends to cause a series of weird problems and special cases in the language semantics, such as functions not being able to return static arrays, and out parameters not being possible to be static arrays.
> 
> Andrei and I agonized over this for some time, and eventually came to the conclusion that static arrays should become value types. I.e.,
> 
>   T[3]
> 
> should behave much as if it were:
> 
>   struct ??
>   {
>      T[3];
>   }
> 
> Then it can be returned from a function. In particular,
> 
>   void foo(T[3] a)
> 
> is currently done (as in C) by passing a pointer to the array, and then with a bit of compiler magic 'a' is rewritten as (*a)[3]. Making this change would mean that the entire array would be pushed onto the parameter stack, i.e. a copy of the array, rather than a reference to it.
> 
> Making this change would clean up the internal behavior of types.
> They'll be more orthogonal and consistent, and templates will work better.
> 
> The previous behavior for function parameters can be retained by making it a ref parameter:
> 
>    void foo(ref T[3] a)

WOO! Thanks! :D
October 20, 2009
Walter Bright wrote:
> Currently, static arrays are (as in C) half-value types and half-reference types. This tends to cause a series of weird problems and special cases in the language semantics, such as functions not being able to return static arrays, and out parameters not being possible to be static arrays.
>
> Andrei and I agonized over this for some time, and eventually came to the conclusion that static arrays should become value types. I.e.,
>
>   T[3]
>
> should behave much as if it were:
>
>   struct ??
>   {
>      T[3];
>   }
>
> Then it can be returned from a function. In particular,
>
>   void foo(T[3] a)
>
> is currently done (as in C) by passing a pointer to the array, and then with a bit of compiler magic 'a' is rewritten as (*a)[3]. Making this change would mean that the entire array would be pushed onto the parameter stack, i.e. a copy of the array, rather than a reference to it.
>
> Making this change would clean up the internal behavior of types. They'll be more orthogonal and consistent, and templates will work better.
>
> The previous behavior for function parameters can be retained by making it a ref parameter:
>
>    void foo(ref T[3] a)

Would that be D1&D2 or only D2?


October 20, 2009
Yes please!
October 20, 2009
Robert Jacques wrote:
> Thank you. Will the various operators be overloaded? i.e. a = b + c; instead of a[] = b[] + c[]; ?

Don't know. For now, I'd just leave the a=b+c syntax illegal. We can always loosen things up later.
October 20, 2009
Saaa wrote:
> Would that be D1&D2 or only D2? 

D2 only, as it changes the behavior.
« First   ‹ Prev
1 2 3 4 5 6