View mode: basic / threaded / horizontal-split · Log in · Help
December 06, 2012
constructor is not callable using argument types ()
I am learning D classes and I am getting error when I am try to 
make instance of class. erorr:
C:\code\main.d(9): Error: constructor 
GetFileName.GetFileName.this (string name) is not callable using 
argument types ()

http://www.everfall.com/paste/id.php?xie09xz9upth
December 06, 2012
Re: constructor is not callable using argument types ()
On 2012-12-06, 20:17, Suliman wrote:

> I am learning D classes and I am getting error when I am try to make  
> instance of class. erorr:
> C:\code\main.d(9): Error: constructor GetFileName.GetFileName.this  
> (string name) is not callable using argument types ()
>
> http://www.everfall.com/paste/id.php?xie09xz9upth

You have supplied the string you promised to.

You have this constructor:

    this(string name)

And call it like this:

    new GetFileName();

Where's the string the constructor expects?

Instead, you should call it like thus:

    new GetFileName("test");

-- 
Simen
December 06, 2012
Re: constructor is not callable using argument types ()
Am I right that my variant of code would be valid for C#?

How can I assign value to name in such manner?
December 06, 2012
Re: constructor is not callable using argument types ()
When I should use keyword this?
I dropped it from my class and now I can make instance of class 
without in sych way:

auto file = new GetFileName();
file.name = "test";
December 06, 2012
Re: constructor is not callable using argument types ()
On 2012-12-06, 20:48, Suliman wrote:

> When I should use keyword this?
> I dropped it from my class and now I can make instance of class without  
> in sych way:
>
> auto file = new GetFileName();
> file.name = "test";

Indeed. If you have not defined a constructor, the language defines one
for you, which is parameterless. It does nothing but allocate and
initialize memory.

The moment you define a constructor of your own, the compiler decides
you probably want to define the parameterless constructor yourself, or
not at all, and thus does not define it for you.

If you want both, then define both:

class MyClass {
    string name;
    this() {
        name = "Foo!";
    }
    this(string name) {
        this.name = name;
    }
}

-- 
Simen
December 08, 2012
Re: constructor is not callable using argument types ()
Could you help me with this simple code. I still playing with 
constructor, but I can't understand how to use it.
http://www.everfall.com/paste/id.php?6n72xxxkz7ba
December 08, 2012
Re: constructor is not callable using argument types ()
Suliman:

> Could you help me with this simple code. I still playing with 
> constructor, but I can't understand how to use it.
> http://www.everfall.com/paste/id.php?6n72xxxkz7ba

You have code like this:

class FileName
{
    string name;

    this(string name)
    {
        this.name = name;
    }
...

void ReadFile()
{
    auto file = new FileName();
...

As you see you define a costructor that accepts a string, but 
then when you allocate the class you don't give it a string.

So the correct code is:

void ReadFile()
{
    auto file = new FileName("somestring");
...



More notes:
- By convention in D method/function names start with a lower 
case.
- Don't indent module-level functions (like ReadFile).
- If your program is composed by more than one module, I suggest 
to add a name at the top of the imported module, like "module 
Foo;".
- In D structs are used quite often.

Bye,
bearophile
December 10, 2012
Re: constructor is not callable using argument types ()
Thanks!

But if I have a lot of property like^
string name
int age
....

it's not good to specify all of them in paranceses:
auto file = new FileName("test.txt", 21, ETC);

How can I specify them in another way?
December 10, 2012
Re: constructor is not callable using argument types ()
Suliman:

> But if I have a lot of property like^
> string name
> int age
> ....
>
> it's not good to specify all of them in paranceses:
> auto file = new FileName("test.txt", 21, ETC);
>
> How can I specify them in another way?

There are several ways to help that. A simple way is to use 
default arguments:

this(string fileName, int id=21, Foo f=ETC) {...}

An alternative is to define various constructors with different 
number of arguments.

Other solutions include the use of a little configuration struct, 
or the chaining of setters that return "this":

FileName setId(int id_) { this.id = id_; return this; }

Then if you define several of such setters, you can chain only 
the ones you want in a single line.

auto f = new FileName("test.txt");
f.setId(21).setF(ETC);

Probably there are other solutions.

Bye,
bearophile
Top | Discussion index | About this forum | D home