Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:41:06+00:00 2026-06-05T02:41:06+00:00

I am implementing a DSL in IronPython. Suppose I have a value hierachy implemented

  • 0

I am implementing a DSL in IronPython.

Suppose I have a value hierachy implemented in C# to be used in iron python:

 public abstract Value
 {

 }

 public abstract DoubleValue : Value
 {
   // Constructors...      

   public double magnitude;

   // Arithmetic operators overloaded...
 }

 public abstract FractionValue : Value
 {
   // Constructors....

   public int numerator;
   public int denominator;

   // Arithmetic operators overloaded...
 }

Due to the operator overloading in C#, i can do this in Python:

 # a, b are of type Value
 def Sum(a,b):
    return a + b

And everything works fine, the function returns an object of type = Value.

But if i want to use a PythonConstant:

 # a is of type Value
 def Sum5(a):
     return a + 5

you get an error type, because the constant 5 is not of Value type.

One solution would be to overload the + operator to work ints like:

public DoubleValue operator+(DoubleValue, int)

but then you get a huge amount of possible combinations and end up with
hundreds of overloads in the Value framework. Anyway you still get
this problem:

def ReturnFive():
    return 5

In this case the returned value is not of Value type, you should do something like:

def ReturnFive():
    return DoubleValue(5.0)

But it is a pretty ugly syntax for my DSL.

What would you recommend?

Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:41:09+00:00Added an answer on June 5, 2026 at 2:41 am

    This is the major issues with DSELs: they don’t always play well with native types. Generally you need to wrap the native types; one option is to introduce a function with a very short name (such as _) that wraps the passed-in value and triggers the operator overloads.

    IronPython only has three number types of interest — System.Int32 (int), System.Double (float), and System.Numerics.BigInteger (long) — so there aren’t too many cases to take care of.

    On the C# side you would have something like:

    class Value {
        static Value WrapLiteral(object literal) {
            if(literal is System.Double) {
                return DoubleValue((System.Double)literal);
            } // etc ...
        }
    }
    

    When you create the scope for your user scripts, add that function with the short name:

    scope.SetVariable("_", Value.WrapLiteral);
    

    Then from the user side, the users only have to do:

    def ReturnFive():
        return _(5)
    

    It’s still a bit ugly, but not too bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Implementing a 'sandbox' environment in Python used to be done with the rexec module
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
When implementing jQuery UI Slider (http://jqueryui.com/demos/slider/) there is always a pre-selection (default value 0).
When implementing move constructors and move assignment operators, one often writes code like this:
I implementing OLE DB provider for my custom database. It will be used from
Implementing nested subcommands in Python with cmdln. I'm not sure I'm using the right
Implementing swing applications, often you realize class of components that should have a coherent
When implementing Quicksort, one of the things you have to do is to choose
I'm implementing in Groovy a DSL for some existing file format. In this format
I have a system implementing Camel and ActiveMQ for communication between some servers. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.