String concatenation

May 5, 2010 at 4:55 PM

I have started to write a simple DLR language and am having problems concatenating strings.

I use Expression.Add to create an exression to concatenate two strings together and get the error message:

The binary operator Add is not defined for the types System.string and System.string.

What is the best way to get around this (can't see how Sympl handles this).

I was thinking using a language specific class to implement strings, with the + operator defined instead of System.String

or is there a better way ?

 

Thanks

 

Coordinator
May 5, 2010 at 5:07 PM

You should just call the String.Concat method and pass it the strings you want concatenated.  String doesn’t actually define operator + on it – it’s simply available in C# due to the compiler special casing string.

From: atomicpigeon [mailto:notifications@codeplex.com]
Sent: Wednesday, May 05, 2010 8:56 AM
To: Dino Viehland
Subject: String concatenation [dlr:211665]

From: atomicpigeon

I have started to write a simple DLR language and am having problems concatenating strings.

I use Expression.Add to create an exression to concatenate two strings together and get the error message:

The binary operator Add is not defined for the types System.string and System.string.

What is the best way to get around this (can't see how Sympl handles this).

I was thinking using a language specific class to implement strings, with the + operator defined instead of System.String

or is there a better way ?

Thanks

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 5, 2010 at 5:32 PM
As dinov says, the quick way out is using String.Concat - I would however recommend you into implementing your own BinaryOp call site binder which would allow you to define the "+" add operation for more then just two strings or some other pre-defined case.
May 6, 2010 at 3:54 PM

Thanks all.

I've implemented this by emiting an Expression.Add for numeric parameters or an Expression.Call calling String.Concat for strings at the code generation stage.

However,  I'm not sure how you would do this with a binder

I've looked at Sympl but can't figure it out.

 

Coordinator
May 6, 2010 at 4:38 PM

Where are you emitting the Add vs. Call today?  Do you have some type info at parse/gen time so that you can do that?

If Sympl were to add this, I'd go into SymplBinaryOperationBinder, and rather than always calling Expression.MakeBinary, I'd look at the left and right meta-object's RuntimeType property.  If they were both string, then I'd return a DynamicMetaObject whose expression was your Call expression, but otherwise, I'd return what Sympl does today.  What might happen then at runtime is someone could try to add 3 and "foo", and the expression would be an error as appropriate (assuming no implicit conversions from int to string).

Bill

From: atomicpigeon [mailto:notifications@codeplex.com]
Sent: Thursday, May 06, 2010 7:55 AM
To: Bill Chiles
Subject: Re: String concatenation [dlr:211665]

From: atomicpigeon

Thanks all.

I've implemented this by emiting an Expression.Add for numeric parameters or an Expression.Call calling String.Concat for strings at the code generation stage.

However, I'm not sure how you would do this with a binder

I've looked at Sympl but can't figure it out.

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 6, 2010 at 5:15 PM

In the language I am implementing the data of a variable is defined in its declaration

eg:

numeric mynumber

string mystring

The source is first parsed to a language specfic expression tree.

The code generator then uses this to generate the DLR expression tree. The Add vs Call decision is done at this stage based on the operand data types.

 

Coordinator
May 6, 2010 at 5:19 PM

Ok, so you don't need to worry about binders in this case, which was my guess, but I figured I'd point out where you'd do it if you needed to do so.

Cheers,

Bill

May 14, 2010 at 5:09 PM

Thanks

Are there any advantages in using binders for a statically typed language ?

Specifically I'm thinking of invoking functions and object members.

Coordinator
May 14, 2010 at 5:44 PM

Answering your question literally, no.  However, if you're ostensibly a static language like C#, and you'd like to interop with dynamic objects or dynamic languages (to make some xml crawling, web page interop, some DB interactions), then you might use binders in some cases.  C# added the 'dynamic' keyword which directs the compiler essentially to NOT statically bind to members on Object, but to runtime bind to the actual type of the object to do operations and member accesses.  Of course, in "normal" mode, C# uses no binders.

Cheers,

Bill

From: atomicpigeon [mailto:notifications@codeplex.com]
Sent: Friday, May 14, 2010 9:09 AM
To: Bill Chiles
Subject: Re: String concatenation [dlr:211665]

From: atomicpigeon

Thanks

Are there any advantages in using binders for a statically typed language ?

Specifically I'm thinking of invoking functions and object members.

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com