- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Constructors in Scala
Sat, 2009-02-28, 10:44
I am new with Scala, as I am reading book "Programming in Scala", I got
question. In Scala we have class parameters. But what if i would like to
declare more then one constructor with different parameter types ?
Sat, 2009-02-28, 12:07
#2
Re: Constructors in Scala
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
Does this snippet from the Scala REPL help?
scala> class C(i: Int, s: String) { def this(n: Int) = this(n, "boo!") }
defined class C
scala> val x = new C(7)
x: C = C@2598a35d
scala> val y = new C(7, "blah!")
y: C = C@7844a013
staar2 wrote:
> I am new with Scala, as I am reading book "Programming in Scala", I got
> question. In Scala we have class parameters. But what if i would like to
> declare more then one constructor with different parameter types ?
- --
Tony Morris
http://tmorris.net/
S, K and I ought to be enough for anybody.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkmpFaMACgkQmnpgrYe6r63DYACfRWY8CHOwInQ4DPLjz3kyPoXJ
/KYAoLbGqOs0Nd26S11xUMpApTNqNnmc
=Swkn
-----END PGP SIGNATURE-----
staar2 wrote:
>
> I am new with Scala, as I am reading book "Programming in Scala", I got
> question. In Scala we have class parameters. But what if i would like to
> declare more then one constructor with different parameter types ?
>
Ok, i got myself answer. You can use auxiliary constructors, if your
defining an auxilary constructor you need to use the primary constructor.