On 23/07/2012, at 1:12 PM, Dobes Vandermeer wrote:
> 
> Hmm this "new" keyword is new to me... is that a felix construct of some 
> sort?  

var px = new 42;
println$ *px;

new (in an expression) just makes a copy of a value
on the heap and returns a pointer to the allocated heap object.

Note that union constructors can do that too:

        union X = | Double d | Int i;
        var a : X = Double 42.1;

probably, the 42.1 is on the heap now.


> I thought things were allocated on the heap by making a pointer to a var, 
> like:
> 
> var x = X(a,b);
> val ptr_x = &x;

No, that just returns a pointer to the variable's storage, the same as in C.
The pointer will be "into a heap object" if and only if the variable x is
already on the heap, that is, contained in a heap object, in this case
the containing function or procedure.

If the containing function is not inlined its closure may 

(a) be on the heap
(b) be on the machine stack via a C++ object
(c) be on the machine stack as a local variable of a C function

The choice is made by the optimiser. If the function is
inlined, the answer depends on the calling function
or procedure.

> 
> 
> You should note that despite the impression a C++ constructor is a function
> its is NOT a function! It is in fact a *procedure* which is passed a pointer
> to allocated store, and some arguments, and then proceeds to initialise
> that store.
> 
> Right, in C++ that is how it works; "this" is a pointer to the already 
> allocated object.

Yep, and you will note that the C++ type system is unsound because "this" is 
always
a pointer to a non-const object: there are two errors here:

(a) the object doesn't have that type .. yet. The constructors job is to 
construct it!
(b) the object may be like

        X const x;

but the pointer is still non-const. So if the constructor "leaks" the this 
pointer
to the outside world you can access a const object as a non-const one.


>  
> > I've always found it annoying in all the languages that use constructors 
> > that they all use same name (usually as the class).  In felix this is 
> > optional as you can construct objects in a generator or function.
> 
> In Felix you can have any number of distinct names for the same type!
> 
> Yes, I've seen this before.  Although, I've wondered (idly) whether one can 
> have a strongly renamed record or tuple type, such that 
> 
> typedef A = int * int;
> typedef B = int * int;
> 
> proc (a:A) ...
> proc (b:B) ...
> 
> Hmm I think "struct" does this for record types, actually ... but for tuples 
> I'm not sure there is anything.  Maybe it's not needed.

You can do something like this .. weirdly .. with the "new" keyword.
Examine src/lib/std/darray.flx to see a usage.

type A = new int * int;

This creates a new abstract type A.  To make a value:

var a : A = _make_A (1,2); // constructs an int * int and coerces it to an A

To get at the representation you use _repr_ operator:

println$ _repr_ a; // prints a coerced to an int * int

The _repr_ overload on A and the _make_A constructor are 
private to the class in which A is defined by "type A = new",
hence outside the class, the type A is fully abstract (defined
by whatever constructors and accessors you put inside
the class for public use).

Unfortunately you cannot pattern match on A "like an int"
no matter what you do, since there are no "custom matches"
in Felix (yet).

Ocaml actually has a "semi-private" operation in which types
can only be constructed by specified functions, but the type
can still be accessed via its representation, that allows
pattern matching. Felix doesn't have this feature at the moment.
It would be easy to implement in the sense of allowing the
_repr_ overload to escape the class by making it public
(but you would still have to use it to "de-abstract" the type
where you wanted access to the representation).

> Shall I go ahead and:
> 
> 1. Eliminate the "copy" ctor for array (since it's a no-op anyway)
> 2. Change the varray constructors that take a collection to be named 
> "to_varray", and rename others as appropriate
> 
> ?  I'm not sure what the implications of this kind of refactoring are ... and 
> how many people are affected by it (<5?)


Well you can try it, and just rebuild and run all the tests,
and fix anything that breaks.

Also you can try just adding new less ambiguous functions first,
so then nothing breaks, deprecate the old ones (a comment
saying "this is going away soon") and remove the old ones
later.

The number of uses of Felix at the moment is low:

Me, You, Mike Maul occasionally and bit of Shayne.. as far
as I know. Every now and then RF pops his head up but I don't
think he's using a modern version of Felix.

> 
> Yes, but the problem is that these things are NOT copy constructors,
> they're typically identity functions.
> 
>  Er, can you clarify what you mean by "identity function" here?

A functional "no-op". Does nothing. Returns its argument, not necessarily
even a copy of it. EH

        ..  identity x .. 

is the same as

        ... x ...

in any code.

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to