Is Constructable As

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Is Constructable As

Seyed H. HAERI (Hossein)
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/
--------------------------------------------------------------------------------------------------------------

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is Constructable As

Naftoli Gugenheim
No. Instead you need to take a function Int => T. For example,

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


On Tue, Apr 25, 2017 at 6:50 PM Seyed H. HAERI (Hossein) <[hidden email]> wrote:
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/
--------------------------------------------------------------------------------------------------------------

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is Constructable As

Seyed H. HAERI (Hossein)
In reply to this post by Seyed H. HAERI (Hossein)
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

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is Constructable As

Seyed H. HAERI (Hossein)
In reply to this post by Naftoli Gugenheim
> No.

Should I get dismayed at that you mean?

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

Sorry. But, that's not interesting.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is Constructable As

Naftoli Gugenheim

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?

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is Constructable As

Seyed H. HAERI (Hossein)
Thank you Naftoli. What I was missing was the way to lift the
constructor. With that, things are all set. :)

On 26 April 2017 at 02:05, Naftoli Gugenheim <[hidden email]> wrote:

> 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), 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/
--------------------------------------------------------------------------------------------------------------

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is Constructable As

Naftoli Gugenheim

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


On Tue, Apr 25, 2017 at 8:15 PM Seyed H. HAERI (Hossein) <[hidden email]> wrote:
Thank you Naftoli. What I was missing was the way to lift the
constructor. With that, things are all set. :)

On 26 April 2017 at 02:05, Naftoli Gugenheim <[hidden email]> wrote:
> 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), 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/
--------------------------------------------------------------------------------------------------------------

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Is Constructable As

Seyed H. HAERI (Hossein)
Indeed. I see I also overlooked that.

On 26 April 2017 at 02:33, Naftoli Gugenheim <[hidden email]> wrote:

> You could also just write the longer form, (i: Int) => new C(i)
>
>
> On Tue, Apr 25, 2017 at 8:15 PM Seyed H. HAERI (Hossein)
> <[hidden email]> wrote:
>>
>> Thank you Naftoli. What I was missing was the way to lift the
>> constructor. With that, things are all set. :)
>>
>> On 26 April 2017 at 02:05, Naftoli Gugenheim <[hidden email]> wrote:
>> > 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), 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/
>>
>> --------------------------------------------------------------------------------------------------------------
>>
>> --
>> 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 [hidden email].
>> For more options, visit https://groups.google.com/d/optout.



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

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/
--------------------------------------------------------------------------------------------------------------

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.