On 27/03/2009, at 12:42 AM, Erick Tryzelaar wrote:

> I was playing around with a simple example, where I tried to add a
> default constructor to a struct, but it doesn't look like structs can
> use the _ctor methods.


That's right, structs can't have any methods. Ctors aren't methods,
since they make an object or value.

Structs come with generated constructors which make them from
tuples. You could also use a record if you want named components.

A user defined constructor is just an ordinary function, but it can't
have the same name in the same scope as a type (including a
struct type) so you need to call it "make_T" or something.


> With this module:
>
> open module Vector3
> {
>  struct vector3 = { x:float; y:float; z:float; };
>
>  fun _ctor_vector3 () => vector3(0.0f, 0.0f, 0.0f);
> }
>
> instance Str[Vector3::vector3] {
>  fun str (v:Vector3::vector3) =>
>    'Vector3::vector3(' +
>    (f'%f, %f, %f' (double v.x, double v.y, double v.z))
>    + ')';
> }
>
>
> This:
>
> val v1 = Vector3::vector3 (1.0f, 2.0f, 3.0f);
> println v1;
>
>
> prints out:
>
> Vector3::vector3(1.000000, 2.000000, 3.000000)
>
> whereas this:
>
> val v0 = Vector3::vector3 ();
> println v0;
>
> errors out with:
>
> CLIENT ERROR
> [lookup_qn_with_sig] Struct constructor for vector3 has wrong  
> signature, got:
> float<2068> * float<2068> * float<2068> -> Vector3::vector3<5144>
> expected:
> unit
> In ./felix/vector.flx: line 3, cols 3 to 50
> 2: {
> 3:   struct vector3 = { x:float; y:float; z:float; };
>     ************************************************
> 4:



Yeah, all this is due to vagaries of the order things are looked up in.
Don't forget to add "apply" methods to all this spaghetti .. :)


--
john skaller
skal...@users.sourceforge.net





------------------------------------------------------------------------------
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to