#1822 One default argument and another not in a function.

Xan Thu 8 Mar 2012

Hi,

I want to do something like this:

class Prova {

 Int suma (Int a:=1, Int b) {
     return a + b
 }

 Void main(Str[] args) {
     if (args.size > 0) {
         suma(3, 45)
     }
     else {
         echo("No arguments")
     }
 }

}

and compiler says me

/home/xan/proves/xitz/xitz-ng/prova.fan(25,22): Parameter 'b' must have default

Why I can't have one default argument and not passing default argument?

Thanks, Xan.

SlimerDude Thu 8 Mar 2012

Only the last arguments can have default values:

Int suma (Int a, Int b := 1) {
  return a + b
}

which makes complete sense, for if any arg could be default and you had a method like:

Int suma (Int a := 1, Int b := 1, Int c) {
  return a + b + c
}

and you wrote

suma(4, 5)

the compiler wouldn't know which args to assign values to! (a,c or b,c?)

Xan Thu 8 Mar 2012

Mmmm... okay, but for me the order is important. Not in this case but in others. I explain: I have

Int Function(Type1 a, Type2 b) {
...
}

I want value1 as default value of a.

I could reverse order, but know I want a

Functionwithdefaulta(Type2 b)

I can't put the same name (Duplicate slot name). So I'm in trouble

For the other hand, the code of Fantom could change for allowing default args in left if there is only one default argument.

My real problem is that:

[Str:Tupla] Tags := [:]
Void SetTag(Str name, Type tipus, Obj objecte) {
     Tags[name] = Tupla(tipus, objecte)
}

and Tupla only contains "Type" and "Obj" fields.

What can I do if I want "Type = Str#" as default?

Thanks in advance, Xan.

SlimerDude Thu 8 Mar 2012

Hmm, I don't think the default args for Fantom are going to change anytime soon! (For that would make the rules for what is and isn't allow far too complicated.)

I don't really understand the problem - defaults are just a lazy way of specifying an argument, similar to method overloading in Java. The calling method could specify your default:

SetTag("name", Str#, myObj) ...

Or if your tuple always has a type you could write your own "defaults" impl:

Void SetTag(Str name, Obj p1 := null, Obj p2 := null) {
  Type t := Str#
  Obj value := null

  if (p1 is Type) {
    t = p1 as Type
    value = p2
  }

  if (p2 is Type) {
     t = p2 as Type
  }

  ... wotever ...

  Tags[name] = Tupla(t, value)
}

Xan Thu 8 Mar 2012

Thanks but I need (Str, Type, Obj) with Str# as Type as defaults. The simplest method is to define

SetTextTag(name,value) as SetTag(name, Str#, value)

but I wonder if I could have the same name. Why in fantom we not be able to have the same name for different slots with different arguments?

Thanks, Xan.

brian Thu 8 Mar 2012

I'm not sure why if you want default arguments, you don't just put them at the end of the parameter list. This is expected paradigm for most languages, and is definitely the Fantom convention. I personally would expect your API to look like this:

setTag(Str name, Obj val, Type type := val.typeof)

SlimerDude Fri 9 Mar 2012

Why in fantom we not be able to have the same name for different slots with different arguments?

See Method overloading

I think the last comment sums it up nicely :

I personally think that no methods/fields overloading is one of the best feature of Fan :)

Login or Signup to reply.