Sorin Miklós Zsejki | 16 Jul 19:46

Is "typecase self of" a syntax error?

If I have:

component Main
export Executable

trait T
    m() = typecase self of
        T => "T"
        else => "else"
    end
end

run(args) = ()

end

I get "/path.../Main.fss:5:24: Syntax Error". Is it an error? If I
have "typecase s = self of" instead, I don't get the error message.

Unrelated to this, I've seen I pass "-Dfortress.static.analysis=1"
when I call fortress. Probably this was documented sometime somewhere.
Does this still have an effect? If so, does it have the same effect as
running typecheck before?

Sukyoung Ryu | 18 Jul 03:27
Favicon

Re: Is "typecase self of" a syntax error?

Hi Sorin,

On Jul 16, 2008, at 1:50 PM, Sorin Miklós Zsejki wrote:

> If I have:
>
> component Main
> export Executable
>
> trait T
>    m() = typecase self of
>        T => "T"
>        else => "else"
>    end
> end
>
> run(args) = ()
>
> end
>
> I get "/path.../Main.fss:5:24: Syntax Error". Is it an error? If I
> have "typecase s = self of" instead, I don't get the error message.

It is indeed a syntax error.  A typecase expression is expected to  
have a binding such as "x = self" or "(x, y, z) = triple".  In  
addition, Fortress provides a syntactic sugar "typecase x of ..." for  
"typecase x = x of ..." when the identifier "x" is an immutable  
variable in scope.  Revision 2301 added a slightly friendlier syntax  
error message for this case.

(Continue reading)

Jan-Willem Maessen | 18 Jul 04:27
Favicon

Re: Is "typecase self of" a syntax error?


On Jul 17, 2008, at 9:27 PM, Sukyoung Ryu wrote:

> Hi Sorin,
>
> On Jul 16, 2008, at 1:50 PM, Sorin Miklós Zsejki wrote:
>
>> If I have:
>>
>> component Main
>> export Executable
>>
>> trait T
>>   m() = typecase self of
>>       T => "T"
>>       else => "else"
>>   end
>> end
>>
>> run(args) = ()
>>
>> end
>>
>> I get "/path.../Main.fss:5:24: Syntax Error". Is it an error? If I
>> have "typecase s = self of" instead, I don't get the error message.
>
> It is indeed a syntax error.  A typecase expression is expected to  
> have a binding such as "x = self" or "(x, y, z) = triple".  In  
> addition, Fortress provides a syntactic sugar "typecase x of ..."  
> for "typecase x = x of ..." when the identifier "x" is an immutable  
(Continue reading)

Sorin Miklós Zsejki | 18 Jul 13:11

Re: Is "typecase self of" a syntax error?

On Fri, Jul 18, 2008 at 5:27 AM, Jan-Willem Maessen
<Janwillem.Maessen@...> wrote:
>
> Admittedly, I would tend to solve problems that want "typecase self" using
> overriding instead.
>

I agree. I stumbled upon this because I tried to imagine how it would
look like to have a trait like this:

trait HasSelf[\Self extends HasSelf[\Self\]\]
   myself(): Self = typecase s = self of
        Self => s
        else => throw SomeException()
    end
end

and then myself() could be used for default implementations of some
methods in some traits that extend HasSelf, are extended themselves
and have a Self parameter passed to them. But so far I didn't find a
use for this and frankly I hate this pattern (passing subtypes as
static parameters) because it makes the code look messy.

Sorin Miklós Zsejki | 18 Jul 04:32

Re: Is "typecase self of" a syntax error?

Hi Sukyoung,

thanks for the answers. I now realize I've read that part of the spec
only superficially.

On Fri, Jul 18, 2008 at 4:27 AM, Sukyoung Ryu <Sukyoung.Ryu@...> wrote:
> Hi Sorin,
>
> On Jul 16, 2008, at 1:50 PM, Sorin Miklós Zsejki wrote:
>
>> If I have:
>>
>> component Main
>> export Executable
>>
>> trait T
>>   m() = typecase self of
>>       T => "T"
>>       else => "else"
>>   end
>> end
>>
>> run(args) = ()
>>
>> end
>>
>> I get "/path.../Main.fss:5:24: Syntax Error". Is it an error? If I
>> have "typecase s = self of" instead, I don't get the error message.
>
> It is indeed a syntax error.  A typecase expression is expected to have a
(Continue reading)

Victor Luchangco | 18 Jul 04:57
Favicon

Re: Is "typecase self of" a syntax error?


On Jul 17, 2008, at 9:27 PM, Sukyoung Ryu wrote:

> On Jul 16, 2008, at 1:50 PM, Sorin Miklós Zsejki wrote:
>
>> If I have:
>>
>> component Main
>> export Executable
>>
>> trait T
>>    m() = typecase self of
>>        T => "T"
>>        else => "else"
>>    end
>> end
>>
>> run(args) = ()
>>
>> end
>>
>> I get "/path.../Main.fss:5:24: Syntax Error". Is it an error? If I
>> have "typecase s = self of" instead, I don't get the error message.
>
> It is indeed a syntax error.  A typecase expression is expected to  
> have a binding such as "x = self" or "(x, y, z) = triple".  In  
> addition, Fortress provides a syntactic sugar "typecase x of ..."  
> for "typecase x = x of ..." when the identifier "x" is an immutable  
> variable in scope.

(Continue reading)

Eric Allen | 18 Jul 17:07
Favicon

Re: Is "typecase self of" a syntax error?


On Jul 17, 2008, at 9:57 PM, Victor Luchangco wrote:
>

> In case you don't want to bind a new variable, you could always write
>
> typecase _ = self of ...
>
> We don't allow arbitrary expressions in place of a binding because  
> we can't guarantee that a reevaluation of the expression will  
> produce something of the same type.

Good point. Of course, in many cases this approach won't apply,  
because we are often interested in treating self with a more precise  
static type in subtraits.

Note that the particular toy example:

>> trait T
>>   m() = typecase self of
>>       T => "T"
>>       else => "else"
>>   end
>> end

can be simply replaced with the expression "T".

BTW, whenever tempted to do a typecase on self, consider using an  
abstract helper method that is overridden in subtraits. For example,  
instead of writing this:
(Continue reading)

Sorin Miklós Zsejki | 18 Jul 17:38

Re: Is "typecase self of" a syntax error?

Good point. But as I said in a previous message, I was thinking what
if T had many such methods and it would become practical to give a
default implementation to most of them, so that creating a U or a V
would not require very much boilerplate.

On Fri, Jul 18, 2008 at 6:07 PM, Eric Allen <Eric.Allen@...> wrote:
>
> On Jul 17, 2008, at 9:57 PM, Victor Luchangco wrote:
>>
>
>> In case you don't want to bind a new variable, you could always write
>>
>> typecase _ = self of ...
>>
>> We don't allow arbitrary expressions in place of a binding because we
>> can't guarantee that a reevaluation of the expression will produce something
>> of the same type.
>
> Good point. Of course, in many cases this approach won't apply, because we
> are often interested in treating self with a more precise static type in
> subtraits.
>
> Note that the particular toy example:
>
>>> trait T
>>>  m() = typecase self of
>>>      T => "T"
>>>      else => "else"
>>>  end
>>> end
(Continue reading)

Victor Luchangco | 18 Jul 18:07
Favicon

Re: Is "typecase self of" a syntax error?

On Jul 18, 2008, at 11:07 AM, Eric Allen wrote:

> BTW, whenever tempted to do a typecase on self, consider using an  
> abstract helper method that is overridden in subtraits.

Yup.  I've never yet been so tempted. :-)  The only reason I can  
think that you might want to do this to if the typecase is a (small)  
part of a larger body, and doesn't correspond to another method  
(especially if the subtraits may be defined in different  
components).  Something like:

trait T
   m() = do
     (* lots of stuff here *)
     typecase self of
       U => u()
       V => v()
       else => ()  (* do nothing *)
     end
     (* more stuff *)
   end

trait U extends T
   u() = ...
end

trait V extends T
   v() = ...
end

(Continue reading)

Sorin Miklós Zsejki | 18 Jul 19:23

Re: Is "typecase self of" a syntax error?

With all this discussion, I realize I don't understand what the static
type of self is. So I tried this:

trait T
    m() = f(self)
end

object O extends T end

f(t: T) = println "f(T)"

f(o: O) = println "f(O)"

run(args): () = O.m()

and it prints "f(O)". Is this the intended behavior? That is, self has
static type O, not T, in this case?

Guy Steele | 18 Jul 20:35
Favicon

Re: Is "typecase self of" a syntax error?


On Jul 18, 2008, at 1:23 PM, Sorin Miklós Zsejki wrote:

> With all this discussion, I realize I don't understand what the static
> type of self is. So I tried this:
>
> trait T
>    m() = f(self)
> end
>
> object O extends T end
>
> f(t: T) = println "f(T)"
>
> f(o: O) = println "f(O)"
>
> run(args): () = O.m()
>
> and it prints "f(O)". Is this the intended behavior? That is, self has
> static type O, not T, in this case?

This is the intended behavior, but remember that Fortress,
unlike Java, does //dynamic// dispatch on all arguments.

The static type of "self" is relevant to deciding which
method signatures are applicable, but then at run time
a dynamic dispatch is performed to select the most
specific applicable overloading.  So you get "f(O)".

--Guy Steele
(Continue reading)

Sorin Miklós Zsejki | 18 Jul 23:25

Re: Is "typecase self of" a syntax error?

On Fri, Jul 18, 2008 at 9:35 PM, Guy Steele <Guy.Steele@...> wrote:
> [...]
> This is the intended behavior, but remember that Fortress,
> unlike Java, does //dynamic// dispatch on all arguments.
>
> The static type of "self" is relevant to deciding which
> method signatures are applicable, but then at run time
> a dynamic dispatch is performed to select the most
> specific applicable overloading.  So you get "f(O)".

This is great. Some threads ago (the subject was: "How to "overload a
function with static parameters"?") I wanted to achieve something, and
I've realized from this response that I can:

component Sample.Main
export Executable

trait P end
trait R[\T extends P\] end
f(p: P) = O(p)

trait SubP extends P end
trait SubR[\T extends SubP\] extends R[\T\] end
f(p: SubP) = SubO(p)

object O[\T extends P\](p: T) extends R[\T\] end
object SubO[\T extends SubP\](p: T) extends SubR[\T\] end

object Po() extends P end
object SubPo() extends SubP end
(Continue reading)

Victor Luchangco | 21 Jul 19:17
Favicon

Re: Is "typecase self of" a syntax error?

On Jul 18, 2008, at 5:25 PM, Sorin Miklós Zsejki wrote:

> This is great. Some threads ago (the subject was: "How to "overload a
> function with static parameters"?") I wanted to achieve something, and
> I've realized from this response that I can:
>
> component Sample.Main
> export Executable
>
> trait P end
> trait R[\T extends P\] end
> f(p: P) = O(p)
>
> trait SubP extends P end
> trait SubR[\T extends SubP\] extends R[\T\] end
> f(p: SubP) = SubO(p)
>
> object O[\T extends P\](p: T) extends R[\T\] end
> object SubO[\T extends SubP\](p: T) extends SubR[\T\] end
>
> object Po() extends P end
> object SubPo() extends SubP end
>
> run(args): () = do
>     typecase _ = f(Po()) of (* I now use the right syntax here ;) *)
>         O[\Po\] => println "O[\\Po\\]"
>         else => println "something else"
>     end
>     typecase _ = f(SubPo()) of
>         SubO[\SubPo\] => println "O[\\SubPo\\]"
(Continue reading)

Sorin Miklós Zsejki | 21 Jul 19:48

Re: Is "typecase self of" a syntax error?

Thank you for the explanation. This was my initial understanding.

So this was the intended behavior, but only for the current interpreter.

On Mon, Jul 21, 2008 at 8:17 PM, Victor Luchangco
<Victor.Luchangco@...> wrote:
> On Jul 18, 2008, at 5:25 PM, Sorin Miklós Zsejki wrote:
>
>> This is great. Some threads ago (the subject was: "How to "overload a
>> function with static parameters"?") I wanted to achieve something, and
>> I've realized from this response that I can:
>>
>> component Sample.Main
>> export Executable
>>
>> trait P end
>> trait R[\T extends P\] end
>> f(p: P) = O(p)
>>
>> trait SubP extends P end
>> trait SubR[\T extends SubP\] extends R[\T\] end
>> f(p: SubP) = SubO(p)
>>
>> object O[\T extends P\](p: T) extends R[\T\] end
>> object SubO[\T extends SubP\](p: T) extends SubR[\T\] end
>>
>> object Po() extends P end
>> object SubPo() extends SubP end
>>
>> run(args): () = do
(Continue reading)


Gmane