Question on CStruct

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Question on CStruct

Thomas Girard
Hello,

Can we use instance variables in CStruct?

The following snippet is working as expected:

> CStruct subclass: Sequence [
>     <declaration: #( (#maximum #uLong)
>                      (#length #uLong)
>                      (#buffer (#ptr #CObject))
>                      (#release #uChar) )>
>
>     | arrayType |
>
>     arrayType [
>         ^arrayType
>     ]
>
>     arrayType: aType [
>         arrayType := aType
>     ]
> ]
>
> Eval [
>     |s|
>     s := Sequence new.
>     s arrayType: CBoolean.
>     s arrayType inspect.
> ]

But embedding this CStruct with instance variable into another CStruct
does *not* work:

> CStruct subclass: EmbeddedSequence [
>     <declaration: #( (#days #uShort)
>                      (#intervals #{Sequence}) )>
> ]
>
> Eval [
>     |eq|
>     eq := EmbeddedSequence new.
>     eq intervals arrayType: CBoolean.
>     eq intervals arrayType inspect.
> ]
because we get an UndefinedObject for arrayType. Somehow it seems the
instance variable arrayType cannot be reached.

I would expect either gst to reject the former snippet if instance
variable in CStruct is not allowed, or to correctly return what was
sent in the second snippet. Am I missing something?

Thanks,
Regards,

Thomas

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Question on CStruct

Paolo Bonzini-2
Il 09/12/2012 22:57, Thomas Girard ha scritto:
> Hello,
>
> Can we use instance variables in CStruct?

Yes.

> But embedding this CStruct with instance variable into another CStruct
> does *not* work:
>> CStruct subclass: EmbeddedSequence [
>>     <declaration: #( (#days #uShort)
>>                      (#intervals #{Sequence}) )>
>> ]
>>
>> Eval [
>>     |eq|
>>     eq := EmbeddedSequence new.
>>     eq intervals arrayType: CBoolean.
>>     eq intervals arrayType inspect.
>> ]
> because we get an UndefinedObject for arrayType. Somehow it seems the
> instance variable arrayType cannot be reached.

It does work.  The problem is that each invocation of #intervals creates
a new instance of Sequence.  This is the definition of the method:

    intervals [
       ^self at: 8 type: (CType cObjectType: Sequence)
    ]

Try this snippet:

    Eval [
        |eq int|
        eq := EmbeddedSequence new.
        int := eq intervals.
        int arrayType: CBoolean.
        int arrayType printNl.
        eq intervals arrayType printNl.
        (int = eq intervals) printNl.
        (int == eq intervals) printNl
    ]

You'll get:

    CBoolean
    nil
    true
    false

Paolo

> I would expect either gst to reject the former snippet if instance
> variable in CStruct is not allowed, or to correctly return what was
> sent in the second snippet. Am I missing something?
>
> Thanks,
> Regards,
>
> Thomas
>


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk