Is Constructable As

19 views
Skip to first unread message

Seyed H. HAERI (Hossein)

unread,
Apr 25, 2017, 6:50:16 PM4/25/17
to scala-user
Dear all,

Is there any way for

trait t {
type T
}

to enforce for T to be constructable say like "new T(Int)"? That is,
for T to have a constructor which takes an Int.

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein), Dr.

Post-Doctoral Research Fellow
Department of Computing Science and Engineering
Catholic University of Louvain
Louvain-la-Neuve, Belgium

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------

Naftoli Gugenheim

unread,
Apr 25, 2017, 7:10:58 PM4/25/17
to Seyed H. HAERI (Hossein), scala-user
No. Instead you need to take a function Int => T. For example,

trait TT {
  type T
  def ctor(i: Int): T
}


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Seyed H. HAERI (Hossein)

unread,
Apr 25, 2017, 7:28:38 PM4/25/17
to scala-user
Hi Brian,

> trait t {
> type T
> def apply(x: Int): T
> }
>
> ...and encountered the problem that apply is in the companion object so would need an explicit proxy defined somewhere.

This is not a solution because it instead is legalising

val foo = new t {...}
val bar = foo(37)

> trait t {
> type T
> val Tc: Int => T
> }

Neither is this. Because, instead of enforcing the availability of a
Int => T constructor for T, it's only enforcing the availability of
such a method in t. The latter is a property of t, the former is that
of T. I'm after the former.

Thank you anyway.

Cheers,
--Hossein

Seyed H. HAERI (Hossein)

unread,
Apr 25, 2017, 7:31:50 PM4/25/17
to scala-user
> No.

Should I get dismayed at that you mean?

> Instead you need to take a function Int => T.

Sorry. But, that's not interesting.

Naftoli Gugenheim

unread,
Apr 25, 2017, 8:06:10 PM4/25/17
to Seyed H. HAERI (Hossein), scala-user

I don’t know who Brian is or what’s not interesting about it. But you can lift the constructor to a function.


scala> trait TT { type T; val ctor: Int => T }
defined trait TT

scala> class TTT(val x: Int)
defined class TTT

scala> new TT { type T = TTT; val ctor = new TTT(_) }
res1: TT{type T = TTT} = $anon$1@59e5ddf

Another way of looking at it is that a constructor of T that takes an Int is a special case of a function Int => T.

To make that more concrete, what if my class is defined like this:

class C(val x: String)
object C {
  def apply(i: Int) = new C(i.toString)
}

People use the Int factory often. It’s a common pattern for users of the above fictional library to write C(10) etc. instead of new C("10").

Now, would you want to disallow using C as your T? Why is the direct constructor more special than the convenience factory?

My solution allows both. Why isn’t that better?

Seyed H. HAERI (Hossein)

unread,
Apr 25, 2017, 8:15:25 PM4/25/17
to scala-user
Thank you Naftoli. What I was missing was the way to lift the
constructor. With that, things are all set. :)
--

Naftoli Gugenheim

unread,
Apr 25, 2017, 8:33:31 PM4/25/17
to Seyed H. HAERI (Hossein), scala-user

You could also just write the longer form, (i: Int) => new C(i)


Seyed H. HAERI (Hossein)

unread,
Apr 25, 2017, 8:40:15 PM4/25/17
to scala-user
Indeed. I see I also overlooked that.
Reply all
Reply to author
Forward
0 new messages