> class <a:type> mysamepair(a): mypair(a, a) { > method swap(): void = ... > } >
I feel that this is a serious problem with typed OOP. Let me give another example which I think no current OOP syntax can handle. Say we have an object of the type myrectangle(x, y), where x and y are of the sort int (for width and height). Imagine a method for doubling the width: fun double_width (obj: !myrectangle(x, y) >> myrectangle(2*x, y)): void This cannot be handled with OOP syntax because OOP syntax all implicitly assumes that the type of 'this' cannot be changed by a method call. Also, one cannot introduce mysquare(x) as a subclass of myrectangle(x, x) because double_with turns a square (of non-zero length) into a non-square rectangle. --Hongwei On Mon, Jan 18, 2021 at 3:51 AM Elijah Stone <elro...@elronnd.net> wrote: > On Sun, 17 Jan 2021, gmhwxi wrote: > > > This time I would like to do something a bit more interesting: > > > > class > > <a:type > > , b:type> > > mygpair(a, b) > > *snip* > > Do we need to say both <a:type,b:type> and (a,b)? It seems redundant, > since we already know the class is parametrized according to a and b, but > maybe I'm missing something? > > > > The mygpair(T1, T2) is a so-called generic class parameterized over > > types T1 and T2. For an object of the type mygpair(T, T), a method for > > swapping the fst and snd components of the object can be supported. But > > this is difficult to do with the OOP syntax. BTW, how would be done in > > Java? Such a method can be readily implemented with the following > > syntax: > > > > fun > > <a:type> > > swap(pp: mygpair(a, a)): void = > > let > > val tmp = pp.fst in pp.fst := pp.snd; pp.snd := tmp > > end > > Hmmm. In a language like c++ where templates are not checked until > they're instantiated, you would be able to just make that a template > method. That doesn't work so well in practice, however... > > One possible solution is subtyping. As in: > > class <a:type,b:type> mypair(a, b) { > var fst: a > var snd: b > //... > } > > class <a:type> mysamepair(a): mypair(a, a) { > method swap(): void = ... > } > > -E > > -- > You received this message because you are subscribed to a topic in the > Google Groups "ats-lang-users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/ats-lang-users/snZWQK2YePY/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > ats-lang-users+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ats-lang-users/7e9a72f0-b2ee-af5f-b297-57f929179ebd%40elronnd.net > . > -- You received this message because you are subscribed to the Google Groups "ats-lang-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/CAPPSPLqCwvGL_2Um%2B45tZSW8HNA%3D4Rir2kTah-1oZNqRHz_Lbw%40mail.gmail.com.