#1664 Proposal for new .. operator

jessevdam Mon 3 Oct 2011

I would like know how the other thinks about the following proposal

Hereby I propose an operator, which allows you to add an extra field to any object, like you can do java script.

When adding a field it looks like (use of := operator)

obj..someNewField := "a value"

When setting the field it looks like (if field does not exist give error)

obj..someNewField = "an other value"

When getting the field it looks like

someVar = obj..someNewField

In JS this can be simply implemented, where this function is already available and the implementation in Java can be done, by adding a hashmap to the fantom Obj class.

alex_panchenko Mon 3 Oct 2011

You can use Dynamic Invoke

jessevdam Mon 3 Oct 2011

That is not the same, the dynamic invoke will delay the type checking to the runtime instead at compile time.

What I mean is that I can add field to a object, while it not defined in the class of the object.

go4 Mon 3 Oct 2011

You could do it by yourself:

class DynamicObj
{
  [Str:Obj?] map := [:]

  override Obj? trap(Str name, Obj?[]? args := null)
  {
    if (args == null) return map[name]
    return map[name] = args[0]
  }
}

obj->someNewField = "a value"
someVar = obj->someNewField

brian Sat 8 Oct 2011

Dynamically (at runtime) I think the -> operator gives you much the flexibility you need. In fact we use this technique for most of our data oriented structures to treat dynamically typed record field access as a -> call.

For compile time stuff, I still think C# static extension methods are the right direction. Just not sure ready to bite that off yet.

Login or Signup to reply.