Re: C API explanation

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

Re: C API explanation

Paolo Bonzini-2

> I've been trying to understand this segment of code:-
>
>             vals := ldap getValues: entry attribute: attr.
>             vals isNull ifFalse: [
>                     val := vals.
>                     [ val value isNil ] whileFalse: [
>                             (attr->val value) printNl.
>                             val incr ].
>             ]
>
> When I look at the orginal C source, vals is just an array of strings
> and it just requires a simple _for_ loop to print out all the values.
> One wonders why the Smalltalk code is so verbose.

Because Smalltalk has no for(A=B;*A;A++) statement:

  val := vals "A=B"
  [val value isNil ] "*A != NULL" whileFalse: [
     ...
     val incr "A++"
  ]

>     val := vals.
>     " why the need to copy val to another variable? Is this just in case
> we decide to update vals?"

To free vals later.

>     [ val value isNil ] whileFalse: [
>     "So what does 'val value' represent? A pointer to an array?"

"val" is A, "val value" is "*A" i.e. the string.

>     (attr->val value) printNl.
>     "'attr->val value'. This is the one I really don't understand. Why
> doesn't the array of strings come into Smalltalk as just that; an array
> of strings, then all one has to to is step through the array? Instead
> we're getting the value from an association..?"

No, that's wrong!  I'm just replacing

  printf ("%s %s\n", attr, *val);

with printing an association.  It's less verbose than

  ('%1 %2' % { attr. val value }) printNl.

but maybe the latter is clearer.

Paolo


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