Nonsense request for constructor?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Nonsense request for constructor?

David Arno

I’m not sure if I’m doing something wrong here, but if I define a class thus:

 

Package x.y;

 

class A {}

 

And then I create an instance of class A elsewhere in the code, I get the following error:

 

src/package/Class.hx:xxxx: characters xx-xx : x.y.A does not have a constructor

 

The class doesn’t have an explicit constructor because none is (should be?) required as it would do nothing. Is there a trick to telling the class it has a default, no action, constructor or does haXe need a pointless boilerplate constructor when it does nothing?

 

Regards,

David.


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Nonsense request for constructor?

singmajesty
I think every class needs a constructor, unless you are extending  
something, in which case the child class could use the parent's constructor



On Tue, 13 Dec 2011 16:27:42 -0800, David Arno <[hidden email]> wrote:

> I'm not sure if I'm doing something wrong here, but if I define a class
> thus:
>
>
> Package x.y;
>
>
> class A {}
>
>
> And then I create an instance of class A elsewhere in the code, I get the
> following error:
>
>
> src/package/Class.hx:xxxx: characters xx-xx : x.y.A does not have a
> constructor
>
>
> The class doesn't have an explicit constructor because none is (should  
> be?)
> required as it would do nothing. Is there a trick to telling the class it
> has a default, no action, constructor or does haXe need a pointless
> boilerplate constructor when it does nothing?
>
>
> Regards,
>
> David.
>


--
Using Opera's revolutionary email client: http://www.opera.com/mail/

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Nonsense request for constructor?

Nicolas Cannasse
In reply to this post by David Arno
Le 14/12/2011 01:27, David Arno a écrit :

> I’m not sure if I’m doing something wrong here, but if I define a class
> thus:
>
> Package x.y;
>
> class A {}
>
> And then I create an instance of class A elsewhere in the code, I get
> the following error:
>
> src/package/Class.hx:xxxx: characters xx-xx : x.y.A does not have a
> constructor
>
> The class doesn’t have an explicit constructor because none is (should
> be?) required as it would do nothing. Is there a trick to telling the
> class it has a default, no action, constructor or does haXe need a
> pointless boilerplate constructor when it does nothing?

There are many valid cases when you don't want to have a default
constructor created when you don't specify one :

- if your class is a collection of static methods (Lambda for example)
there is no meaning of creating an instance of it

- if for some reasons the native API does not allow to directly create
instances for the class (for instance vertex buffers in flash 3D).

- if you want to make sure that your class needs to be extended before
being instanciated (abtract classes)

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Nonsense request for constructor?

Elsass Philippe
While we're talking constructors - could the compiler gently add a missing call to the super contructor instead of just throwing an error?

On Wed, Dec 14, 2011 at 10:50 AM, Nicolas Cannasse <[hidden email]> wrote:
Le 14/12/2011 01:27, David Arno a écrit :

I’m not sure if I’m doing something wrong here, but if I define a class
thus:

Package x.y;

class A {}

And then I create an instance of class A elsewhere in the code, I get
the following error:

src/package/Class.hx:xxxx: characters xx-xx : x.y.A does not have a
constructor

The class doesn’t have an explicit constructor because none is (should
be?) required as it would do nothing. Is there a trick to telling the
class it has a default, no action, constructor or does haXe need a
pointless boilerplate constructor when it does nothing?

There are many valid cases when you don't want to have a default constructor created when you don't specify one :

- if your class is a collection of static methods (Lambda for example) there is no meaning of creating an instance of it

- if for some reasons the native API does not allow to directly create instances for the class (for instance vertex buffers in flash 3D).

- if you want to make sure that your class needs to be extended before being instanciated (abtract classes)

Best,
Nicolas


--
haXe - an open source web programming language
http://haxe.org



--
Philippe

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: Nonsense request for constructor?

David Arno
In reply to this post by Nicolas Cannasse
Hi Nicolas,

I'm not sure I follow the logic of what you are saying. For classes full of
static methods only and classes that must not be directly instantiated, I
can mark the class as @final and create a private constructor.  And I didn't
think that haXe supported abstract classes. How does forcing me to write
unnecessary boilerplate code solve any of those issues? It all smacks of
syntactic salt to me.

Regards,
David.

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Nicolas Cannasse
Sent: 14 December 2011 09:51
To: The haXe compiler list
Subject: Re: [haXe] Nonsense request for constructor?

Le 14/12/2011 01:27, David Arno a écrit :

> I’m not sure if I’m doing something wrong here, but if I define a
> class
> thus:
>
> Package x.y;
>
> class A {}
>
> And then I create an instance of class A elsewhere in the code, I get
> the following error:
>
> src/package/Class.hx:xxxx: characters xx-xx : x.y.A does not have a
> constructor
>
> The class doesn’t have an explicit constructor because none is (should
> be?) required as it would do nothing. Is there a trick to telling the
> class it has a default, no action, constructor or does haXe need a
> pointless boilerplate constructor when it does nothing?

There are many valid cases when you don't want to have a default constructor
created when you don't specify one :

- if your class is a collection of static methods (Lambda for example) there
is no meaning of creating an instance of it

- if for some reasons the native API does not allow to directly create
instances for the class (for instance vertex buffers in flash 3D).

- if you want to make sure that your class needs to be extended before being
instanciated (abtract classes)

Best,
Nicolas

--
haXe - an open source web programming language http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Nonsense request for constructor?

Nicolas Cannasse
In reply to this post by Elsass Philippe
Le 14/12/2011 11:02, Philippe Elsass a écrit :
> While we're talking constructors - could the compiler gently add a
> missing call to the super contructor instead of just throwing an error?

I think this is already the case, at least this is not a stopping error.
Maybe on SVN only.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Nonsense request for constructor?

Nicolas Cannasse
In reply to this post by David Arno
Le 14/12/2011 12:06, David Arno a écrit :
> Hi Nicolas,
>
> I'm not sure I follow the logic of what you are saying. For classes full of
> static methods only and classes that must not be directly instantiated, I
> can mark the class as @final and create a private constructor.  And I didn't
> think that haXe supported abstract classes. How does forcing me to write
> unnecessary boilerplate code solve any of those issues? It all smacks of
> syntactic salt to me.

There is a clear semantic of a class not having a constructor : it can't
be constructed.

I don't think we should use unconventional ways to annotate such
classes, especially to save the enormous amount of boilerplate code
which consists in simply writing :

function new(){}

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Nonsense request for constructor?

Benjamin Dasnois
Particularly since @final indeed has another meaning and is just a meta added kind of as a work-around. You rlogic makes perfect sense Nicolas. Anyway this had already been discussed several times on the list.

On Wed, Dec 14, 2011 at 11:23 AM, Nicolas Cannasse <[hidden email]> wrote:
Le 14/12/2011 12:06, David Arno a écrit :

Hi Nicolas,

I'm not sure I follow the logic of what you are saying. For classes full of
static methods only and classes that must not be directly instantiated, I
can mark the class as @final and create a private constructor.  And I didn't
think that haXe supported abstract classes. How does forcing me to write
unnecessary boilerplate code solve any of those issues? It all smacks of
syntactic salt to me.

There is a clear semantic of a class not having a constructor : it can't be constructed.

I don't think we should use unconventional ways to annotate such classes, especially to save the enormous amount of boilerplate code which consists in simply writing :

function new(){}


Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org



--
DASNOIS Benjamin
http://www.benjamindasnois.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: Nonsense request for constructor?

David Arno
In reply to this post by Nicolas Cannasse
Ah, I see what you are saying now. I had thought that a class had to have a
constructor. Further experiments reveal that if I do not specify a
constructor, I cannot create an instance of the class, but I can reference
static methods of that class for example. I misunderstood your explanation.
Sorry about that.

It's a strange way of doing it, but now I understand what's going on, I
really like it! It certainly seems neater than making the constructor
private to prevent instantiation.

Regards,
David.

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Nicolas Cannasse
Sent: 14 December 2011 11:24
To: The haXe compiler list
Subject: Re: [haXe] Nonsense request for constructor?

Le 14/12/2011 12:06, David Arno a écrit :
> Hi Nicolas,
>
> I'm not sure I follow the logic of what you are saying. For classes
> full of static methods only and classes that must not be directly
> instantiated, I can mark the class as @final and create a private
> constructor.  And I didn't think that haXe supported abstract classes.
> How does forcing me to write unnecessary boilerplate code solve any of
> those issues? It all smacks of syntactic salt to me.

There is a clear semantic of a class not having a constructor : it can't be
constructed.

I don't think we should use unconventional ways to annotate such classes,
especially to save the enormous amount of boilerplate code which consists in
simply writing :

function new(){}

Best,
Nicolas

--
haXe - an open source web programming language http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Nonsense request for constructor?

Simon Krajewski
In reply to this post by Nicolas Cannasse
Am 14.12.2011 12:21, schrieb Nicolas Cannasse:
> Le 14/12/2011 11:02, Philippe Elsass a écrit :
>> While we're talking constructors - could the compiler gently add a
>> missing call to the super contructor instead of just throwing an error?
>
> I think this is already the case, at least this is not a stopping error.
> Maybe on SVN only.

This works:

class A { public function new() { } }
class B extends A { }

But this fails with Missing super constructor call:

class A { public function new() { } }
class B extends A { public function new() { } }

Personally I don't like constructor magic all that much as it gets
confusing fast. Writing super() can be tedious, but its placement within
the constructor can actually have a semantic (i.e. if you assign a value
to a base class variable first, then call its constructor), unlike Java
where the call to super() is necessarily the first one in a subclass. I
think this should be kept visible to the programmer and not get hidden
in constructor magic which might introduce subtle bugs.

Having an IDE check for the extends keyword and auto-generate the super
call is another story and preferable from my point of view.

Regards
Simon


--
haXe - an open source web programming language
http://haxe.org