question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Increase flexibility of constructors, e.g., for pure functional transformations

See original GitHub issue

Description

In the context of pure functional transformations, it would be beneficial to construct OpenXmlElement instances very flexibly. For example, one might want to construct a new element in the following way:

return new Paragraph(
    new ParagraphProperties(new ParagraphStyleId { Val = "MyStyleId" }),
    FunctionReturningIEnumerableOfRun(someParams));

Information

  • .NET Target: All
  • DocumentFormat.OpenXml Version: Latest

See discussion in #627.

Observed

Using the Paragraph class as an example, the subclasses of OpenXmlElement define the following constructors:

public Paragraph()
{
}

public Paragraph(IEnumerable<OpenXmlElement> childElements)
    : base(childElements)
{
}

public Paragraph(params OpenXmlElement[] childElements)
    : base(childElements)
{
}

public Paragraph(string outerXml)
    : base(outerXml)
{
}

With those constructors, the example instance can’t be created, because we can’t combine an OpenXmlElement and an IEnumerable<OpenXmlElement>.

Expected

We should be able to create instances in the way shown in the description.

Design Idea

We could borrow the design from the System.Xml.Linq.XElement class, which, owing to how you can construct instances, is instrumental in pure functional transformations.

The two key XElement constructors for pure functional transformations are:

public XElement(XName name, object content)
    : this(name)
{
    this.AddContentSkipNotify(content);
}

public XElement(XName name, params object[] content)
    : this(name, (object) content)
{
}

Note how the second constructor delegates to the first one. Thus, the magic happens in the AddContentSkipNotify(object) method, which works with pretty much any meaningful actual parameter you throw at it.

Thus, using the Paragraph example, we could replace:

  • Paragraph(IEnumerable<OpenXmlElement>) with Paragraph(object) and
  • Paragraph(params OpenXmlElement[]) with Paragraph(params object[]).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasBarnekowcommented, Dec 19, 2019

@twsouthwick, yes, please do that.

0reactions
ThomasBarnekowcommented, Feb 6, 2020

@twsouthwick, let me explore the extension method route. My initial thought is that this would enable the same approach while only requiring a little more code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Increase flexibility of constructors, e.g., for pure functional ...
Description In the context of pure functional transformations, it would be beneficial to construct OpenXmlElement instances very flexibly.
Read more >
Introduction to pure functional transformations - LINQ to XML
Although functional transformations can be used in many programming scenarios, XML transformation is used here as a concrete example. The ...
Read more >
Simplifying the construction of source code transformations via ...
In addition, a set of example transformations that benefit from the prior application of normalizing restructurings are presented along with a small survey ......
Read more >
Programming Framework
An example of a transformation is one which calculates the mean of a set of ... the benefit is greatly increased flexibility of...
Read more >
Transforming Functional Logic Programs into Monadic ...
a rule defining a function contain only variables and constructor symbols. This restriction also holds for pure functional or logic programs and is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found