Giter Club home page Giter Club logo

Comments (8)

arvidn avatar arvidn commented on July 19, 2024 2

I see. In my mind, that should be typed as:

x = MyInt{f()};

But you won't really get all the type-safety benefits until f() returns MyInt.

from namedtype.

joboccara avatar joboccara commented on July 19, 2024

Hi Arvid,
The intent for the non-const get() method (and the const one as well), was to allow code using NamedType to operate with code that doesn't. For the non-const type, one example would be an assignment to an underlying type. We could argue that we could wrap the value to assign into a NamedType but that could incur a copy, which may or may not be desirable.
Does this seem reasonable to you?

from namedtype.

arvidn avatar arvidn commented on July 19, 2024

I take you specifically refer to the case of traditional code taking a parameter by non-const reference, as an out-parameter. If it's just a matter of casting the result of non-typesafe function into the correct type, one can just use the constructor.

Since out-parameters are being phased out (and generally recommended against) in favor of multiple return values. In my mind, the cost of introducing this loophole in the type-safety is not worth the case of supporting out-parameters. Requiring a copy for out-parameters seem like a reasonable disincentive to use them.

I suppose the other use case would be supporting std::tie() with a function returning the untyped value.

from namedtype.

joboccara avatar joboccara commented on July 19, 2024

Yep, totally in line with you on discouraging out-parameters. The case I was thinking about was like

int f();
using MyInt = NamedType<int, struct MyIntTag>;
MyInt x;
...
x.get() = f();

Does this look like a valid use case to you?

from namedtype.

FlorianWolters avatar FlorianWolters commented on July 19, 2024

I agree with @arvidn, getting rid of the non-const get function leads to cleaner code, imo. Strong types should not be mutable by directly setting the value of the underlying data type.

// Standard Library
#include <iostream>

// NamedType
#include <named_type.hpp>

using Width = fluent::NamedType<double, struct WidthTag>;

double calculateWidth() {
  return 42.0;
}

int main() {
  Width bad_width{0};
  bad_width.get() = calculateWidth();
  std::cout << bad_width.get() << '\n'; // 42.0, WTF!?

  Width good_width{calculateWidth()};
  std::cout << good_width.get() << '\n'; // 42.0, OK!
}

from namedtype.

joboccara avatar joboccara commented on July 19, 2024

Hello Florian,

I get your point. Would it be good to have it as an opt-in skill then? Because having this setter can be handy in some cases, like for introducing strong types in an existing piece of code that performs sets.
Or maybe rather a set() method? Or a set opt-in.

Jonathan

from namedtype.

arvidn avatar arvidn commented on July 19, 2024

my opinion is that along the edges of introducing strong types, it's a feature for the conversions to be verbose and explicit. The two cases I can think of is where a weak type is returned normally and where it's returned as an out-parameter. I think the normal way to deal with that should be:

Width x;
x = Width(calculateWidth());
Width x;
double temp;
calculateWidth(&temp);
x = Width(tmp);

Partly because the out-param case should be a bit extra penalised (because it represents two separate fixes that need to be made).

from namedtype.

viboes avatar viboes commented on July 19, 2024

If the user want to add the operator=(UT) from the underlying type, she can do it as herself, but this is not an operation every strong type should have.

I believe that the access to the underlying type should return a reference/const reference, but the name shouldn't be something friendly. I have used a backdoor idiom to state clearly that the mixin are using something that in principle can be used only by the mixins.

I access it as st._backdoor()._underlying()

If the user wants to declare a get function that return as well a non const reference it is up to the user, or maybe you want to provide a GetAble mixin.

from namedtype.

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.