Why is the def needed in foreach (i: int; def(p.programming_languages[i]))

25 views
Skip to first unread message

Premkumar Devanbu

unread,
Oct 10, 2019, 2:34:26 PM10/10/19
to Boa Language and Infrastructure User Forum
That's my question (in the subject). Isn't it the case that the array will only loop for
the list of languages that actually exist in that project? 

More generally, where and when should one use def? 

Robert Dyer

unread,
Oct 10, 2019, 4:09:52 PM10/10/19
to Boa Language and Infrastructure User Forum
Inside a quantifier (such as foreach), the expression on the RHS is a boolean condition that is evaluated for every possible value of 'i' (or whatever you name the variable).  So the def() function is useful here as it returns a bool and specifically returns True for any value that is 'defined' (think 'not null').  Thus for arrays, the def() effectively allows us to just iterate over every value in the array without filtering any out.

To some degree, this is a mis-use of the intention of the def() function.  def() should be used anywhere one wishes to test if an expression contains a value that is defined.

For example, when you look at the domain-specific types here:  http://boa.cs.iastate.edu/docs/dsl-types.php

you see many attributes marked with a '?' - these are attributes that may or may not be defined.  Here is where you would want to use def() - to test if the attribute exists first before trying to read it.

Hope that helps clarify things!

- Robert
Reply all
Reply to author
Forward
0 new messages