Avoid double validation for constructor arguments that are class pr... (2024)

31 views (last 30 days)

Show older comments

Jan Kappen on 7 Aug 2023

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2005592-avoid-double-validation-for-constructor-arguments-that-are-class-properties

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2005592-avoid-double-validation-for-constructor-arguments-that-are-class-properties

Edited: Matt J on 7 Aug 2023

Open in MATLAB Online

I understand that class properties can be validated via property validation and I love it. I even love more that there's an option to use the meta class to auto-complete and auto-validate class properties like this:

classdef myclass

properties

one

two string {mustBeMember(two, ["only two options are allowed", "option 2"])}

end

methods

function obj = myclass(opts)

arguments

opts.?myclass

end

% assign the properties to the object here

end

end

It results in this, which is beyond awesome, because it auto-completes the properties, but even also shows allowed members of the property. I.e., there's no additional function arguments check required anymore.

Avoid double validation for constructor arguments that are class pr... (2)

But now, assuming that there are not only optional, but one required property like this:

classdef myclass

properties

one

two string {mustBeMember(two, ["only two options are allowed", "option 2"])}

end

methods

function obj = myclass(requiredPropTwo, opts)

arguments

requiredPropTwo

opts.?myclass

end

obj.two = requiredPropTwo;

% assign the properties to the object here

end

end

end

Now, there's no way to auto-complete property two's options anymore:

Avoid double validation for constructor arguments that are class pr... (3)

Of course, assigning the property fails, but Matlab does not understand that there's a direct linkage between my constructor argument the class property.

Avoid double validation for constructor arguments that are class pr... (4)

The only way to get proper auto-completion would be to copy the validation from the property - now we have it doubled which is not a good practice.

function obj = myclass(requiredPropTwo, opts)

arguments

requiredPropTwo string {mustBeMember(requiredPropTwo, ["only two options are allowed", "option 2"])}

opts.?myclass

end

obj.two = requiredPropTwo;

% assign the properties to the object here

end

Avoid double validation for constructor arguments that are class pr... (5)

Questions:

  1. Is it possible to achieve similar behavior as in my first example without copying the validation functions?
  2. Is there a way to auto complete one=..., i.e. using the new argument assignment schema myclass(one=123) instead of myclass("one",123)?
0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Matt J on 7 Aug 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2005592-avoid-double-validation-for-constructor-arguments-that-are-class-properties#answer_1284297

Is it possible to achieve similar behavior as in my first example without copying the validation functions?

No.

Is there a way to auto complete one=..., i.e. using the new argument assignment schema myclass(one=123) instead of myclass("one",123)?

It seems to be there already:

Avoid double validation for constructor arguments that are class pr... (7)

2 Comments

Show NoneHide None

Jan Kappen on 7 Aug 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2005592-avoid-double-validation-for-constructor-arguments-that-are-class-properties#comment_2841597

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2005592-avoid-double-validation-for-constructor-arguments-that-are-class-properties#comment_2841597

Edited: Jan Kappen on 7 Aug 2023

Okay, that means as long as I want to set non-optional class properties via constructor (or other methods) arguments, and want to keep the class properties public settable, I have to duplicate the validation code, is that correct?

> It seems to be there already:

Right.. but not for the first optional name-value-argument, the default auto completion is this:

Avoid double validation for constructor arguments that are class pr... (9)

I can now either push <tab>, and we're in the legacy style of setting parameters, or I can type "one=" which then gets understood by Matlab. The second argument indeed is proposed using the new syntax.

I'd like to just disable auto-completions using the old syntax at all. I'm using the new desktop experiene if that makes a difference.

Thanks!

Edit: I assume, there's no way to re-use function argument validation in function/method calls, too? Described here: Function argument validation and editor auto-completion in wrapper functions/classes - MATLAB Answers - MATLAB Central (mathworks.com)

Matt J on 7 Aug 2023

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2005592-avoid-double-validation-for-constructor-arguments-that-are-class-properties#comment_2841647

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2005592-avoid-double-validation-for-constructor-arguments-that-are-class-properties#comment_2841647

Edited: Matt J on 7 Aug 2023

Open in MATLAB Online

Okay, that means as long as I want to set non-optional class properties via constructor (or other methods) arguments, and want to keep the class properties public settable, I have to duplicate the validation code, is that correct?

Well, only if you want the autocomplete functionality. If all you care about is validation, then this line already intercepts bad input values because of the property validation code,

obj.two = requiredPropTwo;

I assume, there's no way to re-use function argument validation in function/method calls, too?

It would be nice, but I don't think there is. You can write your own customized validation function which can be reused wherever you like, but I don't think Matlab knows how to generate autocompletions for user-defined validation functions, which I think is what you really want.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABProgrammingFunctionsArgument Definitions

Find more on Argument Definitions in Help Center and File Exchange

Tags

  • matlab
  • oop
  • validation functions
  • argument validation

Products

  • MATLAB

Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Avoid double validation for constructor arguments that are class pr... (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Avoid double validation for constructor arguments that are class pr... (2024)

References

Top Articles
Entry Level vs. Executive Costco Membership: What's the Real Difference?
No Costco Membership? No Problem. Here's How You Can Still Shop
Menards Thermal Fuse
Ohio Houses With Land for Sale - 1,591 Properties
1970 Chevrolet Chevelle SS - Skyway Classics
Hawkeye 2021 123Movies
Comcast Xfinity Outage in Kipton, Ohio
Pj Ferry Schedule
Lenscrafters Westchester Mall
AB Solutions Portal | Login
Draconic Treatise On Mining
Legacy First National Bank
Heska Ulite
Umn Biology
Pollen Count Los Altos
Unit 1 Lesson 5 Practice Problems Answer Key
Buying risk?
Hartland Liquidation Oconomowoc
Top tips for getting around Buenos Aires
9044906381
Gino Jennings Live Stream Today
Unterwegs im autonomen Freightliner Cascadia: Finger weg, jetzt fahre ich!
Booknet.com Contract Marriage 2
Lista trofeów | Jedi Upadły Zakon / Fallen Order - Star Wars Jedi Fallen Order - poradnik do gry | GRYOnline.pl
Sodium azide 1% in aqueous solution
The EyeDoctors Optometrists, 1835 NW Topeka Blvd, Topeka, KS 66608, US - MapQuest
Gazette Obituary Colorado Springs
Hampton University Ministers Conference Registration
Masterbuilt Gravity Fan Not Working
Sensual Massage Grand Rapids
Goodwill Of Central Iowa Outlet Des Moines Photos
Superhot Free Online Game Unblocked
LG UN90 65" 4K Smart UHD TV - 65UN9000AUJ | LG CA
Does Circle K Sell Elf Bars
Wake County Court Records | NorthCarolinaCourtRecords.us
Craigslist Neworleans
آدرس جدید بند موویز
Best Weapons For Psyker Darktide
Solemn Behavior Antonym
The Vélodrome d'Hiver (Vél d'Hiv) Roundup
Elizaveta Viktorovna Bout
Former Employees
Amc.santa Anita
Leland Nc Craigslist
30 Years Of Adonis Eng Sub
705 Us 74 Bus Rockingham Nc
Playboi Carti Heardle
Greatpeople.me Login Schedule
Egg Inc Wiki
Hsi Delphi Forum
North Park Produce Poway Weekly Ad
Aspen.sprout Forum
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 5923

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.