Coder Social home page Coder Social logo

Option type about c5 HOT 3 CLOSED

sestoft avatar sestoft commented on August 20, 2024
Option type

from c5.

Comments (3)

sestoft avatar sestoft commented on August 20, 2024

On Mon, 27 Jun 2011, Qbit86 wrote:

Why do you use deprecated out- and ref-parameters to return values?

This is a good idiom in imperative code. In functional code it
obviously is a bad idea.

Peter

from c5.

qbit86 avatar qbit86 commented on August 20, 2024

This is a good idiom in imperative code. In functional code it obviously is a bad idea.

But my C#-code is as functional as possible :) And out/ref + assignments are too imperative :(

You say: «The design has been influenced by the collection libraries for Java and Smalltalk and the published critique of these.» (http://www.itu.dk/research/c5/latest/ITU-TR-2006-76.pdf) It's probably worth looking at collection libraries in mixed-paradigm languages as F# (http://msdn.microsoft.com/en-us/library/ee353413.aspx).
Exception-free search operations always return option as zero-or-one-element collection (in Haskell they return Maybe monad):
http://msdn.microsoft.com/en-us/library/ee353739.aspx
http://msdn.microsoft.com/en-us/library/ee353786.aspx
etc.
It's very handy (even if one cannot use pattern matching in C#): you don't need to divide initialization into declaration and assignment; your locals are immutable, their values are predictable. Compare current usage with suggested:
// 1)
K key = ...
K actualKey = key;
V value;
if (dictionary.Find(ref actualKey, out value)) { /* Use actualKey, value. / }
// 2)
K key = ...
KeyValueOption<K, V> result = dictionary.Find(key);
if (result.HasValue) { /
Use result.Key, result.Value. */ }

In C++ STL we've got similar pattern: get result handle (iterator—Option), compare it with sentinel (v.end()—Option.None), retrieve it from handle (dereferense—Option.Value).

Class Option can be implemented as in Elevate Library (http://elevate.codeplex.com/), for eaxmple.

from c5.

ondfisk avatar ondfisk commented on August 20, 2024

Agreed the Option type of F# is a very neat solution.

However, C5 also tries to mimic the standard C# interfaces and programming styles and here the so called Try Get pattern is most often used. Cf. Krzysztof Cwalina, Brad Abrams: Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries (http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756).

C5 is currently littered with Try Get and it would be an enormous undertaking to convert these into Option type style - and also be a major breaking change it the old Try Get methods were not to remain in the library.

from c5.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.