If structs were even somehow interchangeable with "real" arrays, that might
be a really useful side gain:

$white = new Color(1, 1, 1);

$red = $white->r; // it's a struct

$green = $white['r']; // it's an array

$type = $white['__struct']; // => 'Color'

$array = ['r'=>1, 'g'=>1, 'b'=>1, '__struct'=>'Color'];

$blue = $array->b; // it's an array, but it still works like a struct

You see where I'm going with this?

I imagine the implementation could be something along the lines of checking
for the '__struct' key when somebody attempts to use method-call syntax on
an array, invoking the appropriate method with $this referencing the array
you were using.

The rest of the time, a struct, for all intents and purposes, is just an
array.

I'm sure it's a lot more complex than it sounds, but probably simpler than
attempting to "extend" arrays into structs or trying to "hide" the struct
type-name in an internal array property somehow. Being able to juggle the
types by accessing a reserved key might actually be a benefit?



On Fri, Apr 5, 2013 at 8:50 AM, Rasmus Schultz <ras...@mindplay.dk> wrote:

> > See the "Structs Tutorial" at msdn for a brief summary of structs in C#
> - http://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx
>
> Looking at that code sample, yes - that is more or less exactly what I had
> in mind.
>
> I take back my last remark - I don't think the similarity in syntax is
> confusing; what is confusing, is the way it works now... because there are
> certain types that we just naturally think of as being values, not objects,
> even if they have object syntax.
>
> I don't know C very well, and I certainly don't know the PHP codebase very
> well, but do you think it's feasible to internally piggyback structs on
> arrays?
>
> If structs were even somehow interchangeable with "real" arrays, that
> might be a really useful side gain:
>
>
>
>
>
>
>
> On Fri, Apr 5, 2013 at 4:01 AM, Richard Bradley <
> richard.brad...@softwire.com> wrote:
>
>> > On Thu, Apr 4, 2013 at 12:57 PM, Benjamin Eberlei <kont...@beberlei.de
>> >wrote:
>> >
>> > >
>> > > structs as in c# don't have methods, however DateTime has them. so
>> > > this doesn't work. What you can do is just pass all the data in the
>> > > constructor and then don't change it, and you have your value type
>> that is immutable.
>> > > It just requires a bit self control.
>> >
>> > On 04 April 2013 23:59, Rasmus Schultz <ras...@mindplay.dk> wrote:
>> >
>> > You're right, struct isn't the right word - "value" is probably more
>> accurate.
>>
>> Actually "structs" in C# can have methods. They are exactly classes with
>> value type semantics (i.e. pass-by-value, like arrays in PHP).
>>
>> I think "struct" would be an ideal name for this feature, as this
>> proposal matches the C# implementation very closely, and many people will
>> be familiar with that.
>>
>> See the "Structs Tutorial" at msdn for a brief summary of structs in C# -
>> http://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx
>>
>>
>>
>> Richard Bradley
>> Tel : 020 7485 7500 ext 3230 | Fax : 020 7485 7575
>>
>> softwire
>> Sunday Times Best Small Companies 2012 - 6th in the UK
>> Web : www.softwire.com | Addr : 325 Highgate Studios, 53-79 Highgate
>> Road, London NW5 1TL
>> Softwire Technology Limited. Registered in England no. 3824658.
>> Registered Office : 13 Station Road, London N3 2SB
>>
>>
>

Reply via email to