Seyed H. HAERI (Hossein | 19 Jun 2012 13:19
Picon
Gravatar

How to Demand a Class to Have a Certain Ctor

Dear all,

In my attempt below

trait BaseExp {
  type App <: {def unapply(e: BaseExp, x: String): App}

  def apply(x: String): App = App(this, x)//***
}

I am trying to demand that when e is a BaseExp and x is a String, e(x) should be something of the to-be-materialised-later type App but with the exact contents App(e, x). What I understand from this is that, whilst App should be left to be defined later, a ctor taking a BaseExp and a String should be demanded of it. That's the reason why I put that " <: {def unapply(e: BaseExp, x: String): App} " after "type App" above. The compiler doesn't seem to equate my unapply specification with a constructor however. And, I get the following error for the line with a *** mark:

not found: value App

Whilst this attempt of mine doesn't seem to result, I wonder if there is any way to demand a type to have a ctor of a certain signature?

TIA,
--Hossein

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

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
Nils Kilden-Pedersen | 19 Jun 2012 15:20
Picon
Gravatar

Re: How to Demand a Class to Have a Certain Ctor

You can't. This is what factories are for.

On Tue, Jun 19, 2012 at 6:19 AM, Seyed H. HAERI (Hossein) <hossein.haeri-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Dear all,

In my attempt below

trait BaseExp {
  type App <: {def unapply(e: BaseExp, x: String): App}

  def apply(x: String): App = App(this, x)//***
}

I am trying to demand that when e is a BaseExp and x is a String, e(x) should be something of the to-be-materialised-later type App but with the exact contents App(e, x). What I understand from this is that, whilst App should be left to be defined later, a ctor taking a BaseExp and a String should be demanded of it. That's the reason why I put that " <: {def unapply(e: BaseExp, x: String): App} " after "type App" above. The compiler doesn't seem to equate my unapply specification with a constructor however. And, I get the following error for the line with a *** mark:

not found: value App

Whilst this attempt of mine doesn't seem to result, I wonder if there is any way to demand a type to have a ctor of a certain signature?

TIA,
--Hossein

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

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

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

Seyed H. HAERI (Hossein | 19 Jun 2012 16:22
Picon
Gravatar

Re: How to Demand a Class to Have a Certain Ctor

Hi Nils,

A factory doesn't seem to solve this problem. From what I understand
out of factories, the idea is for me to add a factory method to
BaseExp which takes the appropriate stuff and returns the proper
instantiation. Now, consider this:

trait BaeExp {
  def factory(...): Exp
}

But, what should I put there in the parentheses instead of the
ellipsis? Initial thought: a string plus the argument set. But, what's
the types of the arguments? That varies across concrete types. For
example, my App takes an Exp and a string. Whereas, my Var only takes
a string. So, long story short, what's the correct signature of the
factory method?

TIA,
--Hossein

On 19 June 2012 15:20, Nils Kilden-Pedersen <nilskp@...> wrote:
> You can't. This is what factories are for.
>
>
> On Tue, Jun 19, 2012 at 6:19 AM, Seyed H. HAERI (Hossein)
> <hossein.haeri@...> wrote:
>>
>> Dear all,
>>
>> In my attempt below
>>
>> trait BaseExp {
>>   type App <: {def unapply(e: BaseExp, x: String): App}
>>
>>   def apply(x: String): App = App(this, x)//***
>> }
>>
>> I am trying to demand that when e is a BaseExp and x is a String, e(x)
>> should be something of the to-be-materialised-later type App but with the
>> exact contents App(e, x). What I understand from this is that, whilst App
>> should be left to be defined later, a ctor taking a BaseExp and a String
>> should be demanded of it. That's the reason why I put that " <: {def
>> unapply(e: BaseExp, x: String): App} " after "type App" above. The compiler
>> doesn't seem to equate my unapply specification with a constructor however.
>> And, I get the following error for the line with a *** mark:
>>
>> not found: value App
>>
>> Whilst this attempt of mine doesn't seem to result, I wonder if there is
>> any way to demand a type to have a ctor of a certain signature?
>>
>> TIA,
>> --Hossein
>>
>>
>> --------------------------------------------------------------------------------------------------------------
>>
>> Seyed H. HAERI (Hossein)
>>
>> Research Assistant
>> Institute for Software Systems (STS)
>> Technical University of Hamburg (TUHH)
>> Hamburg, Germany
>>
>> ACCU - Professionalism in programming - http://www.accu.org/
>>
>> --------------------------------------------------------------------------------------------------------------
>
>

--

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

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

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


Gmane