Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am designing a language, and wondering which character (or string) to use to represent that something is an attribute of something else.

In all languages I have used, this is done with a dot - my_class_instance.attribute.

However, in this language, I want to have what would usually be methods as syntactic sugar for normal functions, like so:

x.f(y, z).g(a, b).h(p, q)

is the same thing as

x = f(x, y, z)
x = g(x, a, b)
x = h(x, p, q)

However, if I also use . to show that an attribute belongs to a class I think that will get confusing, so I'm looking for another symbol for that purpose (or another solution).

Currently I am thinking that I should use either the apostrophe - instance'attribute - which shows ownership in a clear way, but means it can't be used for strings, the backtick - instance`attribute - kind of looks like an apostrophe, which is good in the sense it shows ownership, but bad in the sense it could be mistaken for one, and is less commonly typed, and therefore likely to be slower to be typed (and missing from some keyboards).

Alternatively, are there any symbols I could replace the dot used for the syntatic sugar inplace function calling thing with?

share|improve this question

closed as primarily opinion-based by gnat, Steven A. Lowe, MichaelT, Kilian Foth, Dan Pichelman Oct 14 '13 at 14:06

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Doesn't the last assignment win? I don't get why you want to assign to x three times. –  Robert Harvey Oct 12 '13 at 19:37
    
@RobertHarvey Each assignment changes the value of x, so when g is called on x it is different to if it had been called on it at the start, as f(x, y, z) is not necessarily the same as x on that line. –  sweeneyrod Oct 12 '13 at 19:41
    
Naturally each assignment changes the value of x. I can only assume there is some code in between each assignment since the last assignment wins, making the first two assignments pointless. In any case, your example with three lines assigning to x is going to be clearer to most programmers. Clarity is king. –  Robert Harvey Oct 12 '13 at 19:47
2  
@RobertHarvey What do you mean by "wins"? If I do x = 7 then x = x + 5 then x = x * 2 then the last assignment isn't the only important one. –  sweeneyrod Oct 12 '13 at 19:50
3  
hard to believe that you're designing a new OOP language and the only question you have is whether to use a dot or not... –  Steven A. Lowe Oct 12 '13 at 21:19
show 1 more comment

1 Answer

up vote 2 down vote accepted

First of all, the dot-method and dot-attribute syntax isn't mutually exclusive precisely then when you require parens on method calls. Consider this piece of Java:

class Foo {
  int bar = 1;

  int bar() { return 2; }
}

...
Foo foo = new Foo();
foo.bar;   // the attribute
foo.bar(); // the method

Of course, using this language feature can't be recommended. So the real question is how you are going to disambiguate method calls from your “inplace functions”. The probably best way is to use some form of interpolation. Consider this Perl:

my $function = sub {
  my ($object, $arg) = @_;
  return $object + $arg;
};
my $x = 40;
say $x->$function(2); #=> 42

where $x->$y(@z) is syntactic sugar for &$y($x, @z). This syntax is orthogonal to method calls which would look like $x->y or $x->y(). Perl can pull this off because variables have a $ prefix.

Using a similar syntax might be recommendable, so you might end up with

foo.bar;            // attribute
foo.bar();          // method
foo.$bar();         // inplace function
foo.{bar || baz}(); // alternative interpolation marker

You are right, this is confusing. Consider carefully what expressiveness you gain by using inplace functions. In the case of Perl, the variable may contain the name of the method, thus allowing data to decide which method gets called, which in contrast to the above syntactic sugar is actually useful.

$foo->bar;
# the same as
my $meth = "bar";
$foo->$meth;

Your “inplace methods” overload this with very diferent meaning. It would make more sense for x.$y(z) to mean y(x, z) (as the equivalent Perl). Your example with multiple chains would then become

x = x.$f(y, z).$g(a, b).$h(p, q)
// the same as
x = h(g(f(x, y, z), a, b), p, q)

Note that here the .$ apparently has characteristics of a feed operator. In Perl6, these two lines are equivalent:

@input ==> grep { $_ % 3 == 0 } ==> map { $_ * 2 } ==> my @processed;
# the same as
my @processed = map({ $_ * 2 }, grep({ $_ % 3 == 0 }, @input));

Summary: come up with a very good reason why you want to invoke functions as if they were methods, and choose sensible semantics (esp. the auto-assign looks like a bad idea if you've ever programmed functional).
Then, there are various syntactic possibilities to call arbitrary functions on your objects, ranging from “interpolation” x.$y() to operators that emphasize dataflow like ==>.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.