On Mon, Feb 16, 2009 at 12:01 AM, Mike Moretti <[email protected]> wrote:
>
> Hello,
> I am new to factor and want to use it to develop CAD tools. I have developed
> CAD tools for years in other languages such as "C" and scheme.
Great, welcome to the community.
> I will have a lot of instances of tuples with slots containing, what I would
> call, references to instances of other tuples. Does Factor have a similar
> concept of a reference (I.E. pointer)?
Yes. Conceptually, all values are references in Factor. Operations
like 'dup' don't copy objects, they just copy references. If you want
value semantics, you have to explicitly construct new objects or use a
word such as 'clone'.
> That is, if I have an instance of a tuple on the stack and I place it into
> the slot of another tuple will a copy be placed?
No, a reference to that tuple will be placed. Slot setter words >>foo
never copy objects implicitly.
Note that this only matters if you're mutating objects. If you write
code in a more functional style, you won't have to worry about copying
objects -vs- passing references as much.
> I noticed the refs vocabulary, is that something I should be using? That is,
> store a ref to an instance of a tuple?
The refs vocabulary is something else.
> Also, I want to define these slots such that only references to the correct
> tuple can be placed in them. I saw that I could make a predicate class which
> checks the class type and use the predicate class as the type of these slots.
> Is this the best way?
You don't have to define a new predicate class. You can just use a
tuple class. For example,
TUPLE: point x y ;
TUPLE: triangle { a point } { b point } { c point }
If you wanted to, you could elaborate the types of the point too,
TUPLE: point { x float } { y float } ;
Predicate classes are useful if you want a more complex constrant. For
example, suppose you wanted to define a type for polygons,
TUPLE: polygon { points array } ;
But you might want to ensure that only a non-empty array is ever
placed there. So you'd define a predicate class,
PREDICATE: non-empty < array empty? not ;
TUPLE: polygon { points non-empty initial: { { 0 0 } } } ;
Of course, this is probably overkill for this particular example, but
the possibility exists.
Slava
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk