Object Orientation in Loop

33 views
Skip to first unread message

ses

unread,
Jul 19, 2012, 8:13:17 AM7/19/12
to loop...@googlegroups.com
The aspects of both functional and object oriented programming in Loop are clearly carefully chosen. However I wonder if there would be are advantages to introducing the concepts of inheritance and polymorphism integrated in some way. How about having typed functions which can only be called on objects of a particular type, and are automatically selected based on the type provided:

abstract class Person ->
  name
  age

class Employee ->
employeeNumber

class Customer ->
accountNumber

<Person> printPerson(person) ->
print(person.name),
printPersonAux(person)

<Employee> printPersonAux(person) ->
print(person.employeeNumber)

<Customer> printPersonAux(person) ->
print(person.accountNumber)

main ->
print(new Employee(name: 'John', age: 30, employeeNumber: 123))

Would produce the output: John123. You could also do john.printPerson() if 'john' is an instantiated person object.

This is just a fleeting idea, there are many ways object orientation could feature more in Loop, though I understand the desire to keep the language as concise as possible.

Dhanji R. Prasanna

unread,
Jul 19, 2012, 6:50:33 PM7/19/12
to loop...@googlegroups.com
Thanks for the kind words.

Yea I want to add inheritance for objects, this is fairly simple and I would do it in the mixin/trait style, rather than traditional C++-style OO inheritance.

As for the method dispatch polymorphism, the tack I took was using pattern matching instead. This does almost exactly what you suggest in your example:

require prelude as hidden     # hide coz of print()

print(p) =>
  Employee    : ...
  Person        : ...
  Customer    : ...

Instead of thinking of them as separate methods, you can think of them as different flavors based on type. This is available right now in Loop.

Dhanji.

ses

unread,
Jul 23, 2012, 3:39:26 PM7/23/12
to loop...@googlegroups.com
Thanks. I will await the inheritance mechanism with anticipation! The pattern matching makes sense and is compact, though it does still have a feel of conditionally selecting behaviour for types using instanceof in Java. The warm comfort of type safety using generics is hard to let go of!
Reply all
Reply to author
Forward
0 new messages